hdu 4478 Where is the King (BFS)
来源:互联网 发布:农产品数据库 编辑:程序博客网 时间:2024/06/14 12:41
题目分析:给一个n*n的矩阵,.表明可以走,#表明不可以走,给起始位置(x,y),king可以走8个方向,求在t s时,king可能在多少个位置上
注意:1.如果king在第ts到达某个位置 那么在t+2s 肯定还可以到达这个位置,那么对于每个位置可能到达的位置,可能有个到达此位置时的最小偶数even时间,可能有个到达此位置时最小奇数odd时间,所以对于每一个位置,设置两个变量记录这两个值,之后,再扫描一遍,如果t是奇数,t>=odd,ans++;如果t是偶数,t>=even,ans++;
2.由于t<=10^9,很大 不知道如何结束搜索,一直没有想到结束bfs一个节点的方法,参考了这位神的代码码http://blog.csdn.net/binwin20/article/details/8248768,才明白,当一个节点even或者odd被搜到两次,就可以结束了,,,如果他是第一次被搜索到,那么吧这个节点加入队列中去
代码:
#include<iostream>#include<cstdio>#include<memory.h>#include<queue>using namespace std;int maze[110][110];struct node1{ int even; int odd;}vis[110][110];int dir[8][2]={{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};struct node{ int x,y; int t;};int n;queue<node>q;bool IN(int x,int y){ if(1<=x&&x<=n && 1<=y&&y<=n) return true; else return false;}int main(){ int C,t,x,y; scanf("%d",&C); while(C--) { scanf("%d %d %d %d",&n,&t,&x,&y); memset(maze,0,sizeof(maze)); memset(vis,-1,sizeof(vis)); char ch; getchar(); for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { scanf("%c",&ch); if(ch=='.') maze[i][j]=1; } getchar(); } int ans=0,x1,y1; node a; a.x=x; a.y=y; a.t=0; vis[x][y].even=0;/***忘了这个,WA了一次*/ while(!q.empty())/***清空队列****/ q.pop(); q.push(a); while(!q.empty()) { node a1=q.front(); q.pop(); node a2; //printf("x=%d y=%d\n",a1.x,a1.y); for(int i=0;i<8;i++) { a2.x=a1.x+dir[i][0]; a2.y=a1.y+dir[i][1]; if(IN(a2.x,a2.y) && maze[a2.x][a2.y]==1) { a2.t=a1.t+1; if(a2.t%2==0) { if(vis[a2.x][a2.y].even==-1) { vis[a2.x][a2.y].even=a2.t; q.push(a2); } else continue; } else if(a2.t%2==1) { if(vis[a2.x][a2.y].odd==-1) { vis[a2.x][a2.y].odd=a2.t; q.push(a2); } else continue; } } } } for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { if(t%2==0) { if(vis[i][j].even!=-1&&vis[i][j].even<=t) ans++; } else { if(vis[i][j].odd!=-1&&vis[i][j].odd<=t) ans++; } } if(ans==0) ans++; printf("%d\n",ans); } return 0;}/******************错误代码#include<iostream>#include<cstdio>#include<memory.h>#include<queue>using namespace std;int maze[110][110];int vis[110][110];int dir[8][2]={{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};struct node{ int x,y; int t;};int n;queue<node>q;bool IN(int x,int y){ if(1<=x&&x<=n && 1<=y&&y<=n) return true; else return false;}int main(){ int C,t,x,y; scanf("%d",&C); while(C--) { scanf("%d %d %d %d",&n,&t,&x,&y); memset(maze,0,sizeof(maze)); memset(vis,0,sizeof(vis)); char ch; getchar(); for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { scanf("%c",&ch); if(ch=='.') maze[i][j]=1; } getchar(); } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) printf("%d ",maze[i][j]); printf("\n"); } int ans=0,x1,y1; node a; a.x=x; a.y=y; a.t=0; while(!q.empty())//清空队列 q.pop(); q.push(a); while(!q.empty()) { node a1=q.front(); q.pop(); node a2; //printf("x=%d y=%d\n",a1.x,a1.y); for(int i=0;i<8;i++) { a2.x=a1.x+dir[i][0]; a2.y=a1.y+dir[i][1]; //printf("a2.x=%d a2.y=%d\n",a2.x,a2.y); if(IN(a2.x,a2.y) && maze[a2.x][a2.y]==1) { //printf("a2.x=%d a2.y=%d\n",a2.x,a2.y); a2.t=a1.t+1;//a2.t++; if(a2.t==t)//注意判重 { if(vis[a2.x][a2.y]==0)//判重 { ans++; vis[a2.x][a2.y]=1; } continue; } else q.push(a2); } } } if(ans==0) ans++; printf("%d\n",ans); } return 0;}*//***************************************#include<iostream>#include<cstdio>#include<memory.h>#include<queue>using namespace std;int maze[110][110];int vis[110][110];int dir[8][2]={{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};struct node{ int x,y; int t;};int n;queue<node>q;bool IN(int x,int y){ if(1<=x&&x<=n && 1<=y&&y<=n) return true; else return false;}int main(){ int C,t,x,y; scanf("%d",&C); while(C--) { scanf("%d %d %d %d",&n,&t,&x,&y); memset(maze,0,sizeof(maze)); memset(vis,0,sizeof(vis)); char ch; getchar(); for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { scanf("%c",&ch); if(ch=='.') maze[i][j]=1; } getchar(); } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) printf("%d ",maze[i][j]); printf("\n"); } int ans=0,x1,y1; node a; a.x=x; a.y=y; a.t=0; while(!q.empty())//清空队列 q.pop(); q.push(a); while(!q.empty()) { node a1=q.front(); q.pop(); node a2; printf("x=%d y=%d\n",a1.x,a1.y); for(int i=0;i<8;i++) { a2.x=a1.x+dir[i][0]; a2.y=a1.y+dir[i][1]; printf("a2.x=%d a2.y=%d\n",a2.x,a2.y); if(IN(a2.x,a2.y) && maze[a2.x][a2.y]==1 && vis[a2.x][a2.y]==0) { vis[a2.x][a2.y]=1; ans++; a2.t++; if(a2.t>=t) continue; q.push(a2); } } } if(ans==0) ans++; printf("%d\n",ans); } return 0;}*/
- hdu 4478 Where is the King (BFS)
- HDU 4478 Where is the King
- HDU 4478(Where is the King)
- King is the king
- [ACM] hdu 2262 Where is the canteen (高斯消元求期望)
- HDU 2262 Where is the canteen (高斯消元、概率)
- HDOJ 2262 Where is the canteen (高斯消元 + bfs)
- the customer is king?
- where is the marble?(uva 10474)
- Where is the TJH?
- Where is the Marble
- Where is the Marble?
- Where is The Marble
- Where is the Marble?
- Where is the Marble?
- Where is the Marble?
- Where is the Marble?
- Where is the Marble?
- 表达式求值——栈的应用
- 初识Servlet过滤器
- Windows Embedded CE 6.0开发初体验(七)编译和调试平台
- 【quant!quant!!】一家国外金融公司的quant面试题
- GNU ARM 汇编指令
- hdu 4478 Where is the King (BFS)
- 浅谈JAVA中如何利用socket进行网络编程(一)
- 设置iphone中状态栏的状态
- 记录安装oracle的那些事(五)之 还没解决的错误
- javascript难解基础知识
- iframe加载后自适应高度
- 移植vmem_disk 驱动到linux kernel 3.0.0
- hdu 1406 完数
- jquery.bgiframe-2.1.2.js: