尴尬,大写的尴尬!!!

这个题啊,我觉得我的是对的,思路,代码,包括自己YY的案列都没问题

剪枝优化的也比较好,可是答案错误,很是尴尬,网上搜的代码,思路和我的一样,但是他的能过,我的不能,真是呵呵了

算是今天浪费了一天,妈的!!!

 

 

 

这是我的代码:

连连看

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16578    Accepted Submission(s): 4281


Problem Description
“连连看”相信很多人都玩过。没玩过也没关系,下面我给大家介绍一下游戏规则:在一个棋盘中,放了很多的棋子。如果某两个相同的棋子,可以通过一条线连起来(这条线不能经过其它棋子),而且线的转折次数不超过两次,那么这两个棋子就可以在棋盘上消去。不好意思,由于我以前没有玩过连连看,咨询了同学的意见,连线不能从外面绕过去的,但事实上这是错的。现在已经酿成大祸,就只能将错就错了,连线不能从外围绕过。
玩家鼠标先后点击两块棋子,试图将他们消去,然后游戏的后台判断这两个方格能不能消去。现在你的任务就是写这个后台程序。
 

 

Input
输入数据有多组。每组数据的第一行有两个正整数n,m(0<n<=1000,0<m<1000),分别表示棋盘的行数与列数。在接下来的n行中,每行有m个非负整数描述棋盘的方格分布。0表示这个位置没有棋子,正整数表示棋子的类型。接下来的一行是一个正整数q(0<q<50),表示下面有q次询问。在接下来的q行里,每行有四个正整数x1,y1,x2,y2,表示询问第x1行y1列的棋子与第x2行y2列的棋子能不能消去。n=0,m=0时,输入结束。
注意:询问之间无先后关系,都是针对当前状态的!
 

 

Output
每一组输入数据对应一行输出。如果能消去则输出"YES",不能则输出"NO"。
 

 

Sample Input
3 4 1 2 3 4 0 0 0 0 4 3 2 1 4 1 1 3 4 1 1 2 4 1 1 3 3 2 1 2 4 3 4 0 1 4 3 0 2 4 1 0 0 0 0 2 1 1 2 4 1 3 2 3 0 0
 

 

Sample Output
YES NO NO NO NO YES

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int map[1000][1000],map1[1000][1000];
int m,n,q,flag=0;
int i,j,x1,y1,x2,y2;
int f[4][2]={0,1,1,0,0,-1,-1,0},f1[100000];
void dfs(int x,int y,int count,int tep)
{
map1[x][y]=1;
if(count >=3)return ;
if(x==x2-1&&y==y2-1)
{
flag=1;
return ;
}
if(flag)return ;
for(int k=0;k<4;k++)
{
int e=x+f[k][0];
int w=y+f[k][1];
if(e>=0&&e<n&&w>=0&&w<m&&map1[e][w]==0&&(map[e][w]==0||(e==x2-1&&w==y2-1)))
{
f1[tep]=k;
if(tep>0&&f1[tep]!=f1[tep-1])
count++;
if(e==x2-1&&w==y2-1&&count<3)
{
flag=1;

return ;
}
if(flag)return;
dfs(e,w,count,tep+1);
if(flag)return ;
}
}
}
int main()
{
while(scanf("%d %d",&n,&m)&&m&&n)
{
memset(map,0,sizeof(map));
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&map[i][j]);
}
}
scanf("%d",&q);
for(i=0;i<q;i++)
{
memset(map1,0,sizeof(map1));
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
flag=0;
if(map[x1-1][y1-1]!=map[x2-1][y2-1]||map[x1-1][y1-1]==0||map[x2-1][y2-1]==0)
printf("NO\n");
else
{
f1[0]=0;
dfs(x1-1,y1-1,0,0);
if(flag)
printf("YES\n");
else
printf("NO\n");
}
}
}
return 0;
}

 

 

 

 

这是别人的代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <queue>
#include <climits>

using namespace std;

const int MAX = 1003;
const int dirx[5] = {0,0,1,0,-1};
const int diry[5] = {0,1,0,-1,0};

bool visit[MAX][MAX];
int map[MAX][MAX];
int wan[MAX][MAX];
int n,m,bx,by;
bool mark;

bool yes(int x,int y,int dir){
	int dx = bx - x;
	int dy = by - y;
	if(dx!=0)dx = dx/abs(dx);
	if(dy!=0)dy = dy/abs(dy);
	if(dx==dirx[dir] && dy==diry[dir])return true;
	else return false;
}

void dfs(int x,int y,int cnt,int dir){
	int i,tx,ty;
	if(mark)return;
	if(x<1 || y<1 || x>n || y>m || cnt>2)return;
	//注意下面几个剪枝的顺序,顺序搞错了就会出错,因为最后一个元素非0
	if(x==bx && y==by){
		mark = true;
		return;
	}
	if(cnt==2 && !yes(x,y,dir))return;//这个剪枝不强力,加了此剪枝时间只减少了18ms
	if(map[x][y]!=0)return;
	if(wan[x][y]!=-1 && wan[x][y]<=cnt)return;
	wan[x][y] = cnt;
	for(i=1;i<=4;++i){
		tx = x + dirx[i];
		ty = y + diry[i];
		if(dir!=i){
			dfs(tx,ty,cnt+1,i);
		}else{
			dfs(tx,ty,cnt,i);
		}
	}
}

int main(){
//	freopen("in.txt","r",stdin);
	int i,j,t,ax,ay;
	while(scanf("%d %d",&n,&m)!=EOF){
		if(n==0 && m==0)break;
		for(i=1;i<=n;++i){
			for(j=1;j<=m;++j){
				scanf("%d",&map[i][j]);
			}
		}
		scanf("%d",&t);
		while(t--){
			memset(wan,-1,sizeof(wan));
			scanf("%d %d %d %d",&ax,&ay,&bx,&by);
			mark = false;
			if(map[ax][ay]!=map[bx][by] || map[ax][ay]==0){
				printf("NO\n");
				continue;
			}
			wan[ax][ay] = 0;
			for(i=1;i<=4;++i){
				dfs(ax+dirx[i],ay+diry[i],0,i);
			}
			if(mark){
				printf("YES\n");
			}else{
				printf("NO\n");
			}
		}
	}
	
    return 0;
}
posted @ 2017-03-28 21:00  飞起的小田  阅读(283)  评论(0编辑  收藏  举报