Hdu 1072 Nightmare
来源:互联网 发布:mysql约束是什么 编辑:程序博客网 时间:2024/04/28 06:45
这个搜索题有点意思。
需要BFS判重,然后还需要走重复的点。然后我就纠结了。
开始想了用步数来判重,然后想到两个4之间还是会互相推来推去,其实没想清楚
顶多是6,5,4,4,5,6这样就没得推了。有点意思,学习了。
/*ID: Daniel.20PROG: mainLANG: C++*/#include <algorithm>#include <iostream>#include <sstream>#include <string>#include <queue>#include <list>#include <set>#include <map>#include <cmath>#include <vector>#include <cctype>#include <cstring>#include <fstream>using namespace std;const int maxint=0x7FFFFFFF;const long long maxlonglong=0x7FFFFFFFFFFFFFFFLL;char arr[10][10];int mark[10][10];int w,h,n;int result;int sx,sy;int dir[4][2] = {{-1,0},{0,-1},{0,1},{1,0}};struct node{ int x,y,left,total;};bool is_legal(int x, int y){ if(x>=w||x<0||y>=h||y<0) return false; if(arr[x][y]=='0') return false; return true;}int bfs(){ queue<node> q; node s;s.x=sx;s.y=sy;s.left=6,s.total=0; q.push(s); while(!q.empty()){ node t = q.front(); q.pop(); if(arr[t.x][t.y]=='3'){ return t.total; } for(int i=0;i<4;i++){ int nx=t.x+dir[i][0]; int ny=t.y+dir[i][1]; if(is_legal(nx,ny)&&mark[nx][ny]<mark[t.x][t.y]){ cout<<t.x<<t.y<<" "<<nx<<ny<<" "<<mark[t.x][t.y]<<" "<<mark[nx][ny]<<endl; int tmp=t.left-1; if(tmp==0) continue; if(arr[nx][ny]=='4') tmp=6; node next; next.x=nx; next.y=ny; next.left=tmp;next.total=t.total+1; mark[nx][ny]=tmp; q.push(next); } } } return -1;}int main(){ scanf("%d", &n); for(int i=0;i<n;i++){ scanf("%d%d", &w, &h); memset(mark,0,sizeof mark); for(int j=0;j<w;j++){ for(int k=0;k<h;k++){ scanf(" %c", &arr[j][k]); if(arr[j][k]=='2'){ sx=j;sy=k; } } } mark[sx][sy]=6; cout<<bfs()<<endl; } return 0;}
0 0
- HDU 1072 Nightmare
- BFS hdu 1072 Nightmare
- HDU 1072 Nightmare BFS
- HDU 1072 Nightmare
- hdu 1072 Nightmare BFS
- hdu Nightmare 1072
- Hdu 1072 - Nightmare
- hdu 1072 Nightmare bfs
- HDU-1072:Nightmare
- hdu 1072 Nightmare
- HDU 1072 Nightmare
- HDU-1072-Nightmare
- hdu 1072 Nightmare
- HDU 1072 Nightmare
- Nightmare hdu 1072
- hdu 1072 Nightmare
- hdu 1072 Nightmare
- HDU 1072 Nightmare(搜索)
- 【php】微信公众帐号开发接口--消息回复
- eclipse项目上出现两个红点(类似两个红心)的标志
- hdu 1358 Period(KMP)
- Minimum Spanning Tree.prim/kruskal(并查集)
- DataGridView合并单元格
- Hdu 1072 Nightmare
- js中setInterval与setTimeout用法
- Makefile------宏、if条件、shell命令的使用
- Java读取资源文件的那些事
- Linux内核源码分析方法
- linux下面的qq2013...
- ubuntu 12.04中安装thrift-0.9.1
- Unity3D教程宝典之 FX Maker
- CRM上线之路 走上了CRM实施顾问-第123天上班 -第25周