[uva11OPEN]玉米田迷宫Corn Maze(广搜bfs)

第一次做MLE了…第二次WA了5个点,其实就是一个判断错了…QAQ总的来说…是个水题/板子题(逃

#include<bits/stdc++.h>
using namespace std;
#define For(i,l,r) for(register int i=l; i<r; i++)
int n,m;
int d[4][2] = { 0, 1, 0, -1, 1, 0, -1, 0 };
bool bj,vis[301][301];
char ch[301][301];
struct cs{
    int x1,y1,x2,y2;
    bool p;
}chs[35];

struct node {
    int x, y, t;
} q[90001];

inline bool check(int x, int y)
{
  return x >= 0 && y >= 0 && x < n && y < m && vis[x][y] == 0;
} 

int main()
{
  int sx,sy,ex,ey;
  string s;
  scanf("%d%d",&n,&m);
  For(i,0,n){
      cin>>s;
   For(j,0,m){
       if(s[j] == ‘=‘)ex = i, ey = j;
       else if(s[j] == ‘#‘)vis[i][j]=1;
       else if(s[j] == ‘@‘)sx = i, sy = j;
       else if(s[j] >= ‘A‘ && s[j] <= ‘Z‘){
           int c = s[j]-‘A‘;
        ch[i][j] = s[j];
           if (chs[c].p == 1)chs[c].x2 = i, chs[c].y2 = j;  //就是这儿的判断!555查了一晚上_(:3/)__
        else chs[c].x1 = i, chs[c].y1 = j, chs[c].p = 1;
       }
   }
  }
  int head = 0, tail = 1;
  q[tail].x = sx, q[tail].y = sy;
  vis[sx][sy] = 1;
  while(head < tail){
      head++;
      int xn = q[head].x, yn = q[head].y;
      For(i,0,4){
          int dx = d[i][0] + xn;
        int dy = d[i][1] + yn;
        if(check(dx,dy)){
          vis[dx][dy] = 1;
          if(ch[dx][dy] >= ‘A‘ && ch[dx][dy] <= ‘Z‘){
              int c = ch[dx][dy] - ‘A‘;
              if (dx == chs[c].x1 && dy == chs[c].y1)
                dx = chs[c].x2, dy = chs[c].y2;
            else
                dx = chs[c].x1, dy = chs[c].y1;
          }
          q[++tail].x = dx;
          q[tail].y = dy;
          q[tail].t = q[head].t + 1;
          if(dx == ex && dy == ey){
              cout << q[tail].t;
              return 0;
          }
        }
      }
  }
}

原文地址:https://www.cnblogs.com/phemiku/p/11406293.html

时间: 2024-08-02 18:04:23

[uva11OPEN]玉米田迷宫Corn Maze(广搜bfs)的相关文章

洛谷 P1825 [USACO11OPEN]玉米田迷宫Corn Maze

P1825 [USACO11OPEN]玉米田迷宫Corn Maze 题目描述 This past fall, Farmer John took the cows to visit a corn maze. But this wasn't just any corn maze: it featured several gravity-powered teleporter slides, which cause cows to teleport instantly from one point in

[USACO11OPEN]玉米田迷宫Corn Maze

题目描述 This past fall, Farmer John took the cows to visit a corn maze. But this wasn't just any corn maze: it featured several gravity-powered teleporter slides, which cause cows to teleport instantly from one point in the maze to another. The slides w

【luogu P1825 [USACO11OPEN]玉米田迷宫Corn Maze】 题解

题目链接:https://www.luogu.org/problemnew/show/P1825 带有传送门的迷宫问题 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int maxn = 2001; char map[maxn][maxn]; int fx[4] = {0, 1, 0, -1};

poj 3984:迷宫问题(广搜,入门题)

迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7635   Accepted: 4474 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, }; 它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要

算法学习笔记 二叉树和图遍历—深搜 DFS 与广搜 BFS

图的深搜与广搜 马上又要秋招了,赶紧复习下基础知识.这里复习下二叉树.图的深搜与广搜.从图的遍历说起,图的遍历方法有两种:深度优先遍历(Depth First Search), 广度优先遍历(Breadth First Search),其经典应用走迷宫.N皇后.二叉树遍历等.遍历即按某种顺序访问"图"中所有的节点,顺序分为: 深度优先(优先往深处走),用的数据结构是栈, 主要是递归实现: 广度优先(优先走最近的),用的数据结构是队列,主要是迭代实现: 对于深搜,由于递归往往可以方便的利

算法学习笔记(六) 二叉树和图遍历—深搜 DFS 与广搜 BFS

图的深搜与广搜 复习下二叉树.图的深搜与广搜. 从图的遍历说起.图的遍历方法有两种:深度优先遍历(Depth First Search), 广度优先遍历(Breadth First Search),其经典应用走迷宫.N皇后.二叉树遍历等.遍历即按某种顺序訪问"图"中全部的节点,顺序分为: 深度优先(优先往深处走),用的数据结构是栈, 主要是递归实现. 广度优先(优先走近期的).用的数据结构是队列.主要是迭代实现. 对于深搜.因为递归往往能够方便的利用系统栈,不须要自己维护栈.所以通常实

hdu 2717 Catch That Cow(广搜bfs)

题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7909    Accepted Submission(s): 2498 Problem Description Farmer John has been inform

nyist 999 师傅又被妖怪抓走了 【双广搜 || BFS +状态压缩】

题目:nyist 999 师傅又被妖怪抓走了 分析:在一个图中只要看到D点和E点就行的最小步数,看到的定义是:也就是说两个人在同一行或者同一列,并且中间没有障碍物或者没有其他人就可以看到对方. 所以可以先预处理地图,把D点和E点所在的行列的' .'扩展为d和e,然后只要搜到d和e就可以,问题是只有d和e同时搜到才行,直接广搜肯定不行,我们可以在搜到d点之后然后在从当前点广搜e点,或者e点广搜d点,这样第一次搜到的点不一定是最优的,所以需要枚举所有情况才行,时间复杂度较高. 比较好的一种方法是BF

HDU 5336——XYZ and Drops——————【广搜BFS】

XYZ and Drops Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1250    Accepted Submission(s): 407 Problem Description XYZ is playing an interesting game called "drops". It is played on a r∗