hdu 1983(bfs+dfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1983
思路:算得上是暴力解放了。。。orz...由于最多封锁4个区域,因此直接dfs枚举,bfs暴搜即可!!!跑了将近4000ms啊...
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue> 5 using namespace std; 6 #define MAXN 10 7 struct Node{ 8 int x,y,time; 9 int key; 10 }; 11 char map[MAXN][MAXN]; 12 bool mark[MAXN][MAXN][2]; 13 int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}}; 14 int n,m,t; 15 Node st,ed; 16 17 bool bfs(){ 18 memset(mark,false,sizeof(mark)); 19 queue<Node>Q; 20 Node p,q; 21 mark[st.x][st.y][st.key]=true; 22 Q.push(st); 23 while(!Q.empty()){ 24 p=Q.front(); 25 Q.pop(); 26 if(p.time>t)continue; 27 if(map[p.x][p.y]=='E'&&p.key==1){ 28 return false; 29 } 30 for(int i=0;i<4;i++){ 31 q.x=p.x+dir[i][0]; 32 q.y=p.y+dir[i][1]; 33 q.key=p.key; 34 q.time=p.time; 35 if(q.x<1||q.x>n||q.y<1||q.y>m)continue; 36 if(map[q.x][q.y]=='#')continue; 37 if(map[q.x][q.y]=='J')q.key=1; 38 if(!mark[q.x][q.y][q.key]){ 39 q.time+=1; 40 mark[q.x][q.y][q.key]=true; 41 Q.push(q); 42 } 43 } 44 } 45 return true; 46 } 47 48 bool dfs(int total){ 49 if(!total)return bfs(); 50 for(int i=1;i<=n;i++){ 51 for(int j=1;j<=m;j++){ 52 if(map[i][j]=='.'||map[i][j]=='J'){ 53 char ch=map[i][j]; 54 map[i][j]='#'; 55 if(dfs(total-1))return true; 56 map[i][j]=ch; 57 } 58 } 59 } 60 return false; 61 } 62 63 64 int main(){ 65 int _case; 66 scanf("%d",&_case); 67 while(_case--){ 68 scanf("%d%d%d",&n,&m,&t); 69 for(int i=1;i<=n;i++){ 70 scanf("%s",map[i]+1); 71 for(int j=1;j<=m;j++){ 72 if(map[i][j]=='S'){ st.x=i,st.y=j,st.time=0,st.key=0; } 73 } 74 } 75 if(dfs(0)){ puts("0"); } 76 else if(dfs(1)){ puts("1"); } 77 else if(dfs(2)){ puts("2"); } 78 else if(dfs(3)){ puts("3"); } 79 else puts("4"); 80 } 81 return 0; 82 }