【hdu 1175】 连连看 题解 (c++)
2016-04-14 21:49
281 查看
连连看
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 27957 Accepted Submission(s): 6945
Problem Description
“连连看”相信很多人都玩过。没玩过也没关系,下面我给大家介绍一下游戏规则:在一个棋盘中,放了很多的棋子。如果某两个相同的棋子,可以通过一条线连起来(这条线不能经过其它棋子),而且线的转折次数不超过两次,那么这两个棋子就可以在棋盘上消去。不好意思,由于我以前没有玩过连连看,咨询了同学的意见,连线不能从外面绕过去的,但事实上这是错的。现在已经酿成大祸,就只能将错就错了,连线不能从外围绕过。
玩家鼠标先后点击两块棋子,试图将他们消去,然后游戏的后台判断这两个方格能不能消去。现在你的任务就是写这个后台程序。
Input
输入数据有多组。每组数据的第一行有两个正整数n,m(0
#include<iostream> #include<stdio.h> #include<string.h> using namespace std; int arr[1100][1100];//记录原数组 int vis[1100][1100];//记录dfs中有没有搜到过 int n,m; int f; int dfs(int x1,int y1,int x2,int y2,int num,int dd) { // cout<<x1<<" "<<y1<<" "<<num<<endl; if (f==1) return 0; if (num>2) return 0; if (x1==x2 && y1==y2) {f=1;return 0;} int a,b,c,d; a=x1+1; if (a<=n && vis[a][y1]==0) { vis[a][y1]=1; // cout<<"asdfasd"<<endl; if (a==x2 && y1==y2) //find the tar //注意这个过程,就是要找到答案之后更改vis再return 来避免初始化vis的超时 { // cout<<" sss "<<a<<" "<<y1<<" "<<dd<<endl; //cout<<" num is "<<num<<endl; if (dd==3) { f=1; vis[a][y1]=0; //cout<<"================="<<endl; return 0; } else if (dd!=3 && num<=1) { f=1; vis[a][y1]=0; //cout<<"================="<<endl; return 0; } } if (arr[a][y1]==0) if (dd==3)//方向的变换 dfs(a,y1,x2,y2,num,dd); else dfs(a,y1,x2,y2,num+1,3); vis[a][y1]=0; } b=y1+1; if (b<=m && vis[x1][b]==0) { vis[x1][b]=1; if (x1==x2 && b==y2) { if (dd==2 || (dd!=2 && num<=1)) { f=1; vis[x1][b]=0; return 0; } } a6f9 if (arr[x1][b]==0) if (dd==2) dfs(x1,b,x2,y2,num,dd); else dfs(x1,b,x2,y2,num+1,2); vis[x1][b]=0; } a=x1-1; if (a>=0 && vis[a][y1]==0) { vis[a][y1]=1; if (a==x2 && y1==y2) { if (dd==1 || (dd!=1 && num<=1)) { f=1; vis[a][y1]=1; return 0; } } if (arr[a][y1]==0) if (dd==1) dfs(a,y1,x2,y2,num,dd); else dfs(a,y1,x2,y2,num+1,1); vis[a][y1]=0; } b=y1-1; if (b>0 && vis[x1][b]==0) { vis[x1][b]=1; if (x1==x2 && b==y2) { if (dd==4 || (dd!=4 &&num<=1)) { f=1; vis[x1][b]=0; return 0; } } if (arr[x1][b]==0) if (dd==4) dfs(x1,b,x2,y2,num,dd); else dfs(x1,b,x2,y2,num+1,4); vis[x1][b]=0; } } int main() { int a,b,c,d,k; while(cin>>n>>m) { if (n==0) return 0; for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) scanf("%d",&arr[i][j]); scanf("%d",&k); for (int i=1;i<=k;i++) { scanf("%d%d%d%d",&a,&b,&c,&d); f=0; if (arr[a][b]!=arr[c][d] || (arr[a][b]==0 && arr[c][d]==0))//先判断两个点是否相同,相同再搜,或者两个都是0,直接NO {printf("NO\n");continue;} dfs(a,b,c,d,-1,-1); if (f==0) printf("NO\n"); else if (f==1) printf("YES\n"); } } }
相关文章推荐
- [C语言][LeetCode][141]Linked List Cycle
- C语言-郝斌笔记-003数据类型
- 函数的调用优化
- C++——STL所有算法介绍
- C++类型转换:C++风格: static_cast、dynamic_cast、reinterpre
- C语言-郝斌笔记-002病毒程序示范
- [C语言][LeetCode][160]Intersection of Two Linked Lists
- C语言-郝斌笔记-001求二次方程的根
- c++作业3
- C++ STL基本容器的使用
- leetcode101题 题解 翻译 C语言版 Python版
- C语言中以十六进制输出字符型变量会出现'ffffff"的问题
- Teacher_Cadre c++多文件编程
- C语言中的sizeof和strlen
- [C语言][LeetCode][234]Palindrome Linked List
- c++的三次作业
- C语言变量与内存分配
- c++ map、multimap 的使用方法
- [C语言][LeetCode][2]Add Two Numbers
- 关于C++类型检查的一点小挫折