poj-3083-Children of the Candy Corn-DFS+BFS
来源:互联网 发布:丝丽隆鼻 知乎 编辑:程序博客网 时间:2024/06/14 19:06
一个感觉超级恶心的搜索题。—–==—–
题目大意:
给一个图,从S走到E,求三组解,第一组是一直靠着左边走需要的步数,第二组是一直靠着右边走需要的步数,第三组是最短路径,对于第三组,比较简单直接bfs就行了。前两组就有点复杂了,要确定搜索方向啊。。【萌新在这里卡了好久好久】
我们假设初始位置是向上的,并且给上右下左四个方向依次编号为0,1,2,3
一直靠左边走 :
朝向 顺序
上 3 0 1 2
右 0 1 2 3
下 1 2 3 0
左 2 3 0 1
同理可以推出靠右边走的搜索顺序。
贴一下代码:
#include <iostream>#include <cstdio>#include <memory.h>using namespace std;struct point {int x;int y;int step;}Q[45*45];char Map[45][45];int vis[45][45];int dx[]={-1,0,1,0};int dy[]={0,1,0,-1};int h,w,sx,sy;int ans1,ans2,ans3,flagL,flagR,flag;bool check(int x,int y){ if (Map[x][y]!='#'&&x>=1&&x<=h&&y>=1&&y<=w&&!vis[x][y]) return true; return false;}void init(){ memset(vis,0,sizeof(vis)); flag=0; flagR=0; flagL=0; ans1=1; ans2=1; ans3=0;}void DFSL(int x,int y,int tmp){ if (flagL) return; if (Map[x][y]=='E') {flagL=1;return ;} // vis[x][y]=1;不能标记。。。 ans1++; switch(tmp) { case 0: if (check(x,y-1)) DFSL(x,y-1,3); if (check(x-1,y)) DFSL(x-1,y,0); if (check(x,y+1)) DFSL(x,y+1,1); if (check(x+1,y)) DFSL(x+1,y,2); break; case 1: if (check(x-1,y)) DFSL(x-1,y,0); if (check(x,y+1)) DFSL(x,y+1,1); if (check(x+1,y)) DFSL(x+1,y,2); if (check(x,y-1)) DFSL(x,y-1,3); break; case 2: if (check(x,y+1)) DFSL(x,y+1,1); if (check(x+1,y)) DFSL(x+1,y,2); if (check(x,y-1)) DFSL(x,y-1,3); if (check(x-1,y)) DFSL(x-1,y,0); break; case 3: if (check(x+1,y)) DFSL(x+1,y,2); if (check(x,y-1)) DFSL(x,y-1,3); if (check(x-1,y)) DFSL(x-1,y,0); if (check(x,y+1)) DFSL(x,y+1,1); break; }}void DFSR (int x,int y,int tmp){ if (flagR) return ; if (Map[x][y]=='E') {flagR=1;return;} // vis[x][y]=1;这里不能标记啊,深搜要搜到底【一条路走到黑】,走到头了,要回来。。 ans2++; switch(tmp) { case 0: if (check(x,y+1)) DFSR(x,y+1,1); if (check(x-1,y)) DFSR(x-1,y,0); if (check(x,y-1)) DFSR(x,y-1,3); if (check(x+1,y)) DFSR(x+1,y,2); case 1: if (check(x+1,y)) DFSR(x+1,y,2); if (check(x,y+1)) DFSR(x,y+1,1); if (check(x-1,y)) DFSR(x-1,y,0); if (check(x,y-1)) DFSR(x,y-1,3); case 2: if (check(x,y-1)) DFSR(x,y-1,3); if (check(x+1,y)) DFSR(x+1,y,2); if (check(x,y+1)) DFSR(x,y+1,1); if (check(x-1,y)) DFSR(x-1,y,0); case 3: if (check(x-1,y)) DFSR(x-1,y,0); if (check(x,y-1)) DFSR(x,y-1,3); if (check(x+1,y)) DFSR(x+1,y,2); if (check(x,y+1)) DFSR(x,y+1,1); }}void BFS (){ int head=0,hail=0; Q[head].x=sx; Q[head].y=sy; Q[head].step=1; while (head<=hail) { for (int i=0;i<4;i++) { int new_x=Q[head].x+dx[i]; int new_y=Q[head].y+dy[i]; if (check(new_x,new_y)) { Q[++hail].x=new_x; Q[hail].y=new_y; Q[hail].step=Q[head].step+1; vis[new_x][new_y]=1; } if (Map[new_x][new_y]=='E') { ans3=Q[hail].step; flag=1; break; } } if (flag) break; head++; }}int main(){ int T; scanf("%d",&T); while (T--) { init(); scanf("%d %d",&w,&h); getchar(); for (int j=1;j<=h;j++){ for (int k=1;k<=w;k++) { scanf("%c",&Map[j][k]); if (Map[j][k]=='S'){sx=j,sy=k;} } getchar(); } DFSL(sx,sy,0); DFSR(sx,sy,0); BFS(); cout<<ans1<<" "<<ans2<<" "<<ans3<<endl; }}
0 0
- POJ 3083 Children of the Candy Corn dfs+bfs
- poj 3083 Children of the Candy Corn(dfs+bfs)
- POJ 3083Children of the Candy Corn(DFS*2+BFS)
- POJ 3083 Children of the Candy Corn (bfs+dfs)
- POJ 3083:Children of the Candy Corn(DFS+BFS)
- POJ 3083 - Children of the Candy Corn(BFS+DFS)
- POJ 3083 Children of the Candy Corn (DFS + BFS)
- POJ 3083-Children of the Candy Corn(dfs+bfs)
- poj 3083 Children of the Candy Corn(bfs+dfs)
- poj 3083 Children of the Candy Corn (DFS+BFS)
- POJ 3083 Children of the Candy Corn(bfs+dfs)
- poj 3083 Children of the Candy Corn dfs+bfs
- poj-3083 Children of the Candy Corn-DFS+BFS
- Poj 3083 Children of the Candy Corn (DFS+BFS)
- POJ 3083 Children of the Candy Corn(BFS + DFS)
- [Poj 3083] Children of the Candy Corn bfs+dfs
- [POJ 3083]Children of the Candy Corn[DFS+BFS]
- POJ 3083Children of the Candy Corn(BFS+DFS)
- asyncio 官方文档
- Linux下的硬链接和软链接
- OpenGL 的空间变换(下):空间变换
- (最小生成树)ZOJ 1406 Jungle Roads
- 如何修改jar包中的class文件
- poj-3083-Children of the Candy Corn-DFS+BFS
- 【hibernate】多对一,一对多关系
- SVN服务器搭建与使用三
- hdu 1175 连连看(dfs+剪枝做法)
- [cpp]C++11学习笔记-filesystem
- 装饰器的注释
- PostgreSQL 中文手册
- 友元类,成员函数做友元函数
- Vi Editor Stray Problem Sloved