ZOJ-3581 A Simple Test 模拟连连看

n*m的矩阵,其中有k个格子是有图案的,q个询问,如果每次询问的两个格子上都有图案,且可以通过最多变相两次到达(路上不能有其他有图案的格子),这两个格子的图案并得到两分,否则-1分。

其实仔细想想就是连连看的游戏模式,比赛中觉得搜索太暴力会T没敢尝试,结果其实暴力写法也才80ms就过了。

直接暴力模拟能不能满足条件就可以了。

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <algorithm>
using namespace std;
const int maxn=333;
int map[maxn][maxn];
int n,m;
int k;
int q;
int ans;
int x1,y1;
int x2,y2;
int fx[maxn],sx[maxn];
int fy[maxn],sy[maxn];
bool solve()
{
	memset(fx,0,sizeof(fx));
	memset(sx,0,sizeof(sx));
	memset(fy,0,sizeof(fy));
	memset(sy,0,sizeof(sy));
	fx[x1]=1;
	sx[x2]=1;
	for(int i=x1+1;i<m;i++)
	{
		if(map[i][y1]==0)
		{
			fx[i]=1;
		}
		else
		{
			break;
		}
	}
	for(int i=x1-1;i>=0;i--)
	{
		if(map[i][y1]==0)
		{
			fx[i]=1;
		}
		else
		{
			break;
		}
	}
	for(int i=x2+1;i<m;i++)
	{
		if(map[i][y2]==0)
		{
			sx[i]=1;
		}
		else
		{
			break;
		}
	}
	for(int i=x2-1;i>=0;i--)
	{
		if(map[i][y2]==0)
		{
			sx[i]=1;
		}
		else
		{
			break;
		}
	}
	for(int i=0;i<m;i++)
	{
		if(sx[i]&&fx[i])
		{
			bool flag=1;
			for(int j=min(y1,y2)+1;j<max(y1,y2);j++)
			{
				if(map[i][j])
				{
					flag=0;
					break;
				}
			}
			if(flag)
			{
				return true;
			}
		}
	}
	fy[y1]=1;
	sy[y2]=1;
	for(int i=y1+1;i<n;i++)
	{
		if(map[x1][i]==0)
		{
			fy[i]=1;
		}
		else
		{
			break;
		}
	}
	for(int i=y1-1;i>=0;i--)
	{
		if(map[x1][i]==0)
		{
			fy[i]=1;
		}
		else
		{
			break;
		}
	}
	for(int i=y2+1;i<n;i++)
	{
		if(map[x2][i]==0)
		{
			sy[i]=1;
		}
		else
		{
			break;
		}
	}
	for(int i=y2-1;i>=0;i--)
	{
		if(map[x2][i]==0)
		{
			sy[i]=1;
		}
		else
		{
			break;
		}
	}
	for(int i=0;i<n;i++)
	{
		if(fy[i]&&sy[i])
		{
			bool flag=1;
			for(int j=min(x1,x2)+1;j<max(x2,x1);j++)
			{
				if(map[j][i])
				{
					flag=0;
					break;
				}
			}
			if(flag)
			{
				return true;
			}
		}
	}
	return false;
}
int main()
{
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        ans=60;
        memset(map,0,sizeof(map));
        scanf("%d",&k);
        for(int i=0;i<k;i++)
        {
            scanf("%d%d",&x1,&y1);
            map[x1][y1]=1;
        }
        scanf("%d",&k);
        while(k--)
        {
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            if(map[x1][y1]==0||map[x2][y2]==0)
			{
				ans--;
			}
            else if(solve())
            {
                ans+=2;
                map[x1][y1]=0;
                map[x2][y2]=0;
            }
            else
			{
				ans--;
			}
            printf("%d\n",ans);
        }
    }
    return 0;
}
时间: 2024-10-23 23:59:46

ZOJ-3581 A Simple Test 模拟连连看的相关文章

ZOJ 1111 Poker Hands --复杂模拟

昨天晚上写的,写了一个多小时,9000+B,居然1A了,爽. 题意:玩扑克,比大小.规则如下: 题意很简单,看过赌神的人都知道,每人手中5张排,比牌面大小,牌面由大到小分别是(这里花色无大小),级别从高到低依次为:1.同花顺:牌面一样,只比较最大的看谁大,一样大则平手2.四条:四个一样的,看这四个一样的中谁大谁赢3.葫芦:三条+一对,看三条中谁的牌面大4.同花:都是同花就按从大到小比较,看谁的更大5.顺子:都是顺子看最大的谁大,一样则平手6.三条:三条看三条中谁的牌面大7.两对: 两对就看谁的对

zoj 3818 Pretty Poem (模拟)

ZOJ Problem Set - 3818 Pretty Poem Time Limit: 2 Seconds      Memory Limit: 65536 KB Poetry is a form of literature that uses aesthetic and rhythmic qualities of language. There are many famous poets in the contemporary era. It is said that a few ACM

zoj 1763 A Simple Question of Chemistry

A Simple Question of Chemistry Time Limit: 2 Seconds      Memory Limit: 65536 KB Your chemistry lab instructor is a very enthusiastic graduate student who clearly has forgotten what their undergraduate Chemistry 101 lab experience was like. Your inst

ZOJ 3700 Ever Dream(模拟)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3700 Ever Dream Time Limit: 2 Seconds      Memory Limit: 65536 KB  "Ever Dream" played by Nightwish is my favorite metal music. The lyric (see Sample Input) of this song is much

ZOJ 3686 A Simple Tree Problem(线段树)

A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each node has a boolean (0 or 1) labeled on it. Initially, all the labels are 0. We define this kind of operation: given a subtree, negate all its labels. An

ZOJ 3818 Pretty Poem (暴力模拟 string(substr))

Pretty Poem Time Limit: 2 Seconds      Memory Limit: 65536 KB Poetry is a form of literature that uses aesthetic and rhythmic qualities of language. There are many famous poets in the contemporary era. It is said that a few ACM-ICPC contestants can e

CodeForces - 344B Simple Molecules (模拟题)

CodeForces - 344B Simple Molecules Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description Mad scientist Mike is busy carrying out experiments in chemistry. Today he will attempt to join three atoms into

zoj 3314 CAPTCHA(纯模拟)

题目 有些人用深搜写的,当然我这弱弱的,只理解纯模拟... 纯模拟,第一次写了那么长的代码,我自己也是够坚韧不拔的,,,,必须留念啊!!! 注意,G包含C,E包含L,R包含P,(照图说O应该不包含C,但是不排除掉这种情况,就wa掉了,所以要排除O包含C的情况..) #include<stdio.h> #include<string.h> int n,m; char s2[330][330]; int vis[30]; void fun(){ //a if(vis[0]==0){ f

ZOJ 3326 An Awful Problem 模拟

只有在 Month 和 Day 都为素数的时候才能得到糖 那就模拟一遍时间即可. //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <fstream> #include <cstring> #include <cmath> #include <sta