CSU 1604: SunnyPig
来源:互联网 发布:90后流行网络女歌手 编辑:程序博客网 时间:2024/06/07 21:08
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1604
bfs
题意:一只猪要出去,最少要开几扇门,要注意对于不同的门只能朝一个方向打开,而出口的地方,必然有一扇门
#include <iostream> #include <stdio.h> #include <string.h> #include <stack> #include <queue> #include <map> #include <set> #include <vector> #include <math.h> #include <algorithm> using namespace std; #define ls 2*i #define rs 2*i+1 #define up(i,x,y) for(i=x;i<=y;i++) #define down(i,x,y) for(i=x;i>=y;i--) #define mem(a,x) memset(a,x,sizeof(a)) #define w(a) while(a) #define LL long long const double pi = acos(-1.0); #define Len 200005 #define mod 19999997 const int INF = 0x3f3f3f3f; #define exp 1e-8 struct node { int x,y,door; }; int mat[1000][1000],n,m,t,vis[1000][1000],sx,sy; char str[1005]; int to[5][2]= {0,0,0,1,1,0,0,-1,-1,0}; bool outdoor(int x,int y) { if(x==0||y==0||x==n-1||y==m-1) return true; return false; } bool check(int x,int y) { if(x<0||y<0||x==n||y==m) return true; if(mat[x][y]==-1||vis[x][y]) return true; return false; } void bfs() { queue<node> Q; node a,next; int i,j; a.x = sx; a.y = sy; a.door = 0; vis[sx][sy] = 1; Q.push(a); while(!Q.empty()) { a = Q.front(); Q.pop(); if(outdoor(a.x,a.y)&&mat[a.x][a.y]>0) { printf("%d\n",a.door); return ; } up(i,1,4) { next = a; next.x+=to[i][0]; next.y+=to[i][1]; if(check(next.x,next.y)) continue; if(mat[next.x][next.y]>0 && mat[next.x][next.y]!=i) continue; if(mat[next.x][next.y]>0) { next.door++; if(outdoor(next.x,next.y)) { printf("%d\n",next.door); return ; } } vis[next.x][next.y] = 1; Q.push(next); } } printf("-1\n"); } int main() { int i,j,k; scanf("%d",&t); w(t--) { scanf("%d%d\n",&n,&m); n=2*n+1; m=2*m+1; mem(mat,-1); up(i,0,n-1) { gets(str); up(j,0,m-1) { if(str[j]==' '||str[j]=='*') mat[i][j]=0; else if(str[j]=='-'||str[j]=='|') mat[i][j]=-1; else if(str[j]=='E') mat[i][j]=1; else if(str[j]=='S') mat[i][j]=2; else if(str[j]=='W') mat[i][j]=3; else if(str[j]=='N') mat[i][j]=4; else if(str[j]=='O') { sx = i; sy = j; mat[i][j]=0; } } } bfs(); } return 0; }
0 0
- CSU 1604 SunnyPig(BFS)
- CSU 1604: SunnyPig
- CSU1604 sunnypig
- CSU1604: SunnyPig(BFS)
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- java中 i++ 与 ++i
- nyoj801 哈夫曼编码,输入有毒
- Android的内存机制和常见泄漏情形
- uva10791 Minimum Sum LCM
- Android测试系列之Instrumented Unit Test-Espresso
- CSU 1604: SunnyPig
- 国务院关于建立城镇职工基本医疗保险制度的决定
- poj3682 概率
- tcpdump&pt-query-digest分析mysql负载性能问题
- 个人学习-java-final关键字
- c++ gui qt4 chap02 gotocell
- bfc与自适应笔记
- 黑格尔经典名言
- 谈谈深度学习中的 Batch_Size