dfs/poj3083 Children of the Candy Corn

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<queue>
  4
  5 using namespace std;
  6 typedef pair<int,int>P;
  7 const int dx[4]={1,0,-1,0};
  8 const int dy[4]={0,1,0,-1};
  9 const int INF=1e9;
 10 int sx,sy,ex,ey,n,m;
 11 char a[50][50];
 12 int d[50][50];
 13
 14 bool pd(int x,int y)
 15 {
 16     if (x>=0 && x<n && y>=0 && y<m && a[x][y]!=‘#‘) return true;
 17     return false;
 18 }
 19
 20 int dfs(int x,int y,int xx,int yy,char d)
 21 {
 22     if (x==xx && y==yy) return 1;
 23     if (d==‘U‘)
 24     {
 25         if (pd(x-1,y)) return dfs(x-1,y,xx,yy,‘L‘)+1;
 26         else if (pd(x,y-1)) return dfs(x,y-1,xx,yy,‘U‘)+1;
 27         else if (pd(x+1,y)) return dfs(x+1,y,xx,yy,‘R‘)+1;
 28         else if (pd(x,y+1)) return dfs(x,y+1,xx,yy,‘D‘)+1;
 29     }
 30     else if (d==‘L‘)
 31     {
 32         if (pd(x,y+1)) return dfs(x,y+1,xx,yy,‘D‘)+1;
 33         else if (pd(x-1,y)) return dfs(x-1,y,xx,yy,‘L‘)+1;
 34         else if (pd(x,y-1)) return dfs(x,y-1,xx,yy,‘U‘)+1;
 35         else if (pd(x+1,y)) return dfs(x+1,y,xx,yy,‘R‘)+1;
 36     }
 37     else if (d==‘D‘)
 38     {
 39         if (pd(x+1,y)) return dfs(x+1,y,xx,yy,‘R‘)+1;
 40         else if (pd(x,y+1)) return dfs(x,y+1,xx,yy,‘D‘)+1;
 41         else if (pd(x-1,y)) return dfs(x-1,y,xx,yy,‘L‘)+1;
 42         else if (pd(x,y-1)) return dfs(x,y-1,xx,yy,‘U‘)+1;
 43     }
 44     else if (d==‘R‘)
 45     {
 46         if (pd(x,y-1)) return dfs(x,y-1,xx,yy,‘U‘)+1;
 47         else if (pd(x+1,y)) return dfs(x+1,y,xx,yy,‘R‘)+1;
 48         else if (pd(x,y+1)) return dfs(x,y+1,xx,yy,‘D‘)+1;
 49         else if (pd(x-1,y)) return dfs(x-1,y,xx,yy,‘L‘)+1;
 50     }
 51 }
 52
 53 int bfs(int sx,int sy,int ex,int ey)
 54 {
 55     int v[50][50];
 56     for (int i=0;i<50;i++)
 57         for (int j=0;j<50;j++) d[i][j]=INF;
 58
 59     queue<P> que;
 60     que.push(P(sx,sy));
 61     d[sx][sy]=1;
 62     while (que.size())
 63     {
 64         P p=que.front();
 65         que.pop();
 66         if (p.first==ex && p.second==ey) break;
 67         for (int i=0;i<4;i++)
 68         {
 69             int nx=p.first+dx[i];
 70             int ny=p.second+dy[i];
 71             if (pd(nx,ny) && d[nx][ny]==INF)
 72             {
 73                 que.push(P(nx,ny));
 74                 d[nx][ny]=d[p.first][p.second]+1;
 75             }
 76        }
 77     }
 78     return d[ex][ey];
 79 }
 80 int main()
 81 {
 82     int tot;
 83     scanf("%d",&tot);
 84     for(int t=1;t<=tot;t++)
 85     {
 86         scanf("%d%d",&m,&n);
 87         for (int i=0;i<n;i++)
 88         {
 89             scanf("%s",a[i]);
 90             for(int j=0;j<m;j++)
 91             {
 92                 if (a[i][j]==‘S‘)
 93                 {
 94                     sx=i;
 95                     sy=j;
 96                 }else
 97                 if (a[i][j]==‘E‘)
 98                 {
 99                     ex=i;
100                     ey=j;
101                 }
102             }
103         }
104       //  printf("%d %d\n",ex,ey);
105         int ans2=dfs(sx,sy,ex,ey,‘U‘);
106         int ans1=dfs(ex,ey,sx,sy,‘U‘);
107         int ans3=bfs(sx,sy,ex,ey);
108         printf("%d %d %d\n",ans1,ans2,ans3);
109     }
110     return 0;
111 }
时间: 2024-12-20 19:01:29

dfs/poj3083 Children of the Candy Corn的相关文章

POJ3083——Children of the Candy Corn

Children of the Candy Corn DescriptionThe cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, chainsaw-wielding psychopaths, hippies, and other terrors on their quest to find t

poj3083 Children of the Candy Corn BFS&amp;&amp;DFS

Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11215   Accepted: 4841 Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombie

POJ-3083 Children of the Candy Corn (BFS+DFS)

Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, chainsaw-wielding psychopaths, hippies, and other terrors on their quest to find the exit. One popular maze-

poj3083 Children of the Candy Corn

这道题有深搜和广搜.深搜还有要求,靠左或靠右.下面以靠左为例,可以把简单分为上北,下南,左西,右东四个方向.向东就是横坐标i不变,纵坐标j加1(i与j其实就是下标).其他方向也可以这样确定.通过上一步方向可以确定下一步应该从哪个方向开始搜.比如说,是向北走的,就必须先搜西,西不可以走,再搜北,如果北还不可以走,再搜东,最后才是南.其他方向的情况也可以这样推出来.最后走到E点完成了.广搜就是最基础的广搜.这道题做了将近10个小时.中途曾几次准备放弃,但最后还是坚持做完了. #include<ios

POJ 3083:Children of the Candy Corn(DFS+BFS)

Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9311 Accepted: 4039 Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, ch

POJ 3083 Children of the Candy Corn(顺时针DFS+逆时针DFS+BFS)

题目链接:POJ 3083 Children of the Candy Corn [题意]给出一个迷宫,不超过40*40,'#'代表墙,'.'代表能走,'S'是起点,'E'是终点.分别求出从起点一直沿左走,一直沿右走,走到终点所需要的步数.以及走出迷宫的最小步数. [思路]首先最小步数很简单,一个普通BFS搞定,这道题重点是一直向左走和一直向右走的DFS的方向问题,方向还和游客当时朝向有关.开始一直认为是每次都向左(右)转,直到可以走,然后就一直不对,在google了之后才知道向左走要遵循左上右

poj 3083 Children of the Candy Corn(bfs+dfs)

Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10739   Accepted: 4626 Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombie

POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE

POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 2)再输出右转优先时,从S到E的步数 3)最后输出S到E的最短步数 解题思路: 前两问DFS,转向只要控制一下旋转方向就可以 首先设置前进方向对应的数字 向上--N--0 向右--E--1 向下--S--2 向左--W--3 比如说右转优先,即为向右,向前,向左,向后,即逆时针方向for(int i

【POJ 3083】Children of the Candy Corn

POJ[3083]Children of the Candy Corn Dfs+Bfs 分别求沿左墙到达E 沿右墙到达E 还有S到E的最短步数 前两个Dfs实现 最后一个Bfs 耐心写很容易A 主要注意方向问题 dir四个方向 上右下左 刚开始我分别用下标0 1 2 3代表 开dirx diry两个移动数组 假设前一状态朝向0(上) 沿左墙移动即为3 0 1 2(左上右下<顺时针>) 沿右墙即为1 0 3 2(右上左下<逆时针>) 同理其余方向很容易遍历 略自豪的是不断精简代码从6