http://acm.nyist.net/JudgeOnline/problem.php?pid=58
来源:互联网 发布:淘宝资生堂官网旗舰店 编辑:程序博客网 时间:2024/05/18 01:18
bfs搜索水题进行时~~~~
#include<iostream>#include<string.h>#include<cstdio>#include<string>#include<queue>using namespace std;int map[9][9]={1,1,1,1,1,1,1,1,1, 1,0,0,1,0,0,1,0,1, 1,0,0,1,1,0,0,0,1, 1,0,1,0,1,1,0,1,1, 1,0,0,0,0,1,0,0,1, 1,1,0,1,0,1,0,0,1, 1,1,0,1,0,1,0,0,1, 1,1,0,1,0,0,0,0,1, 1,1,1,1,1,1,1,1,1};int dx[]={0,0,1,-1};int dy[]={1,-1,0,0};struct point{int x;int y;int step;}po;bool visit[10][10];int sx,sy,ex,ey;int step;int bfs(int x,int y){memset(visit,false,sizeof(visit));queue<point>q;po.x=x;po.y=y;po.step=0;q.push(po);visit[x][y]=true;while(!q.empty()){po=q.front();q.pop();x=po.x;y=po.y;step=po.step;if(x==ex&&y==ey) return step;for(int i=0;i<4;++i){int x1=x+dx[i];int y1=y+dy[i];if(!map[x1][y1]&&!visit[x1][y1]&&x1>=0&&x1<=8&&y1>=0&&y1<=8){ po.x=x1;po.y=y1;po.step=step+1;q.push(po); visit[x1][y1]=true;}}}}int main(){int T;cin>>T;while(T--){cin>>sx>>sy>>ex>>ey;cout<<bfs(sx,sy)<<endl;}return 0;}
dfs
#include<iostream>#include<string.h>#include<cstdio>#include<string>#include<queue>using namespace std;int map[9][9]={1,1,1,1,1,1,1,1,1, 1,0,0,1,0,0,1,0,1, 1,0,0,1,1,0,0,0,1, 1,0,1,0,1,1,0,1,1, 1,0,0,0,0,1,0,0,1, 1,1,0,1,0,1,0,0,1, 1,1,0,1,0,1,0,0,1, 1,1,0,1,0,0,0,0,1, 1,1,1,1,1,1,1,1,1};int dx[]={0,0,1,-1};int dy[]={1,-1,0,0};struct point{int x;int y;int step;}po;int sx,sy,ex,ey;int step,minstep;void dfs(int x,int y){if(x==ex&&y==ey) {minstep=min(step,minstep);return;}//结束条件 for(int i=0;i<4;++i){int x1=dx[i]+x;int y1=dy[i]+y;if(x1>=0&&x1<=8&&y1>=0&&y1<=8&&!map[x1][y1]){step++;map[x1][y1]=1;dfs(x1,y1);map[x1][y1]=0;//回溯。。。。step--;}}}int main(){int T; scanf("%d",&T);while(T--){ step=0; minstep=0xffffff;scanf("%d%d%d%d",&sx,&sy,&ex,&ey);map[sx][sy]=1;dfs(sx,sy);map[sx][sy]=0;printf("%d\n",minstep);}return 0;}
- http://acm.nyist.net/JudgeOnline/problem.php?pid=58
- http://acm.nyist.net/JudgeOnline/problem.php?pid=290
- http://acm.nyist.net/JudgeOnline/problem.php?pid=434
- http://acm.nyist.net/JudgeOnline/problem.php?pid=13
- http://acm.nyist.net/JudgeOnline/problem.php?pid=239
- http://acm.nyist.net/JudgeOnline/problem.php?pid=129
- http://acm.nyist.net/JudgeOnline/problem.php?pid=431
- http://acm.nyist.net/JudgeOnline/problem.php?pid=301
- http://acm.nyist.net/JudgeOnline/problem.php?pid=451
- http://acm.nyist.net/JudgeOnline/problem.php?pid=459
- http://acm.nyist.net/JudgeOnline/problem.php?pid=20
- http://acm.nyist.net/JudgeOnline/problem.php?pid=267
- http://acm.nyist.net/JudgeOnline/problem.php?pid=305
- http://acm.nyist.net/JudgeOnline/problem.php?pid=309
- http://acm.nyist.net/JudgeOnline/problem.php?pid=325
- http://acm.nyist.net/JudgeOnline/problem.php?pid=420
- http://acm.nyist.net/JudgeOnline/problem.php?pid=488
- http://acm.nyist.net/JudgeOnline/problem.php?pid=150
- seq_file的用法
- C++语言及网络编程书籍整理
- 解决开机打印“03-07 12:02:23.039 E/AudioHardwareMSM8660( 130): unknown stream”的问题。
- HDOJ 1040 As Easy As A+B 快排
- 爱情就是一物降一物——金庸教你谈恋爱
- http://acm.nyist.net/JudgeOnline/problem.php?pid=58
- Powerpc Register Classification Notes and instruction sets .
- MATLAB中Legend的一些控制方法
- JPA 批注参考
- 大数的n次方
- C介绍资料
- 理解Linux系统负荷
- 深入理解C语言指针的奥秘
- Linq to sql 实现多条件的动态查询(方法一)