stud-2779裸bfs
来源:互联网 发布:电力代维软件 编辑:程序博客网 时间:2024/06/05 10:17
c++的数组下标为负数竟然不报错
#include <cstdio>#include <cstring>#include <vector>#include <queue>#include <iostream>using namespace std;#define DEBUGconst int maxn=15+5;char buf[maxn][maxn];bool visited[maxn][maxn];int md[4][2]={0,1,0,-1,-1,0,1,0};int m,n;struct node{ int x,y,step; node(int x,int y,int step):x(x),y(y),step(step){}};void bfs(int x,int y,int t){ queue<node> q; node u(x,y,0); memset(visited,0,sizeof(visited)); visited[x][y]=1; q.push(u); while(!q.empty()){ node u=q.front();q.pop(); visited[u.x][u.y]=1; for(int i=0;i<4;i++){ node temp(u.x+md[i][0],u.y+md[i][1],u.step+1); if(temp.x<0||temp.x>=m||temp.y<0||temp.y>=n)continue; if(visited[temp.x][temp.y]){continue;} if(buf[temp.x][temp.y]=='*'){q.push(temp);visited[temp.x][temp.y]=1;} else if(buf[temp.x][temp.y]=='Y'){printf("%d\n",temp.step);return;} else{ cout<<temp.x<<"\t"<<temp.y<<"\t"<<temp.step<<endl; } } u=q.front(); } printf("%d\n",-1); // for(int i=0;i<4;i++){ // bfs(x+md[i][0],y+md[i][1],t); // }}void solve(){ int sx,sy,ex,ey; for(int i=0;i<m;i++) for(int j=0;j<n;j++) if(buf[i][j]=='X') sx=i,sy=j; else if(buf[i][j]=='Y') ex=i,ey=j; bfs(sx,sy,0);}int main(){#ifdef DEBUG freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);#endif while(scanf("%d%d",&m,&n)==2&&m&&n){ for(int i=0;i<m;i++)scanf("%s",buf[i]); solve(); }#ifdef DEBUG fclose(stdin); fclose(stdout);#endif return 0;}
阅读全文
0 0
- stud-2779裸bfs
- Good stud
- STUD 1613 超级楼梯
- ubuntu下安装android-stud
- 猴子分桃(stud ACM题目)
- Asteroids!-裸的BFS
- BFS
- bfs
- BFS
- BFS
- bfs
- BFS
- bfs
- BFS
- BFS
- bfs
- BFS
- BFS
- HDUoj 1568 Fibonacci ( 数学
- selenium webdriver调用google chrome配置chromedriver
- JAVA企业面试题精选 Web基础 1-10
- 匈牙利算法模板 二分图最大匹配
- 深度学习与神经网络
- stud-2779裸bfs
- 《零基础入门学习Python》学习过程笔记【38类的继承】
- 【Leetcode】【python】Path Sum II
- POJ 3692 Kindergarten
- 一行代码解决各种IE兼容问题,IE6,IE7,IE8,IE9,IE10
- inux 安装虚拟机VMware-Workstation需要权限
- 在Linux下安装PHP过程中,编译时出现configure: error: libjpeg.(a|so) not found 错误的解决办法
- 常用 Git 命令清单
- web方向学习清单