hdu 149 胜利大逃亡(续)

来源:互联网 发布:it have 还是it has 编辑:程序博客网 时间:2024/05/18 14:15

http://acm.hdu.edu.cn/showproblem.php?pid=1429

第一次接触位运算,被坑了好久,一直不知道怎么标记,代码有点乱

#include<stdio.h>#include<queue>#include<string.h>using namespace std;int n,m,t;char map[20][20];int mov[4][2] = {1,0,0,1,-1,0,0,-1};struct node{int x,y,step;int letter;};bool book[20][20][1026];int bfs(int sx,int sy){memset(book,0,sizeof(book));queue<node>q;node p1,p2;p1.x = sx;p1.y = sy;p1.step = 0;p1.letter = 0;q.push(p1);while(!q.empty()){p1 = q.front();q.pop();int next_x,next_y;for(int i = 0;i<4;i++){p2 = p1;next_x = p1.x+mov[i][0];next_y = p1.y+mov[i][1];if(p1.step+1>=t)break;if(next_x<0 || next_y<0 || next_x>n-1 || next_y>m-1)continue;if(map[next_x][next_y]=='^')return p1.step+1;if(map[next_x][next_y]<='z' && map[next_x][next_y]>='a'){if(book[next_x][next_y][p1.letter | 1<<(map[next_x][next_y]-'a')] == true)continue;p2.letter = (p1.letter | 1<<(map[next_x][next_y]-'a'));}if(map[next_x][next_y]<='Z' && map[next_x][next_y]>='A'){if((p1.letter & 1<<(map[next_x][next_y]-'A'))==0)continue;}if(book[next_x][next_y][p1.letter] == false && map[next_x][next_y]!='*'){p2.step = p1.step+1;p2.x = next_x;p2.y = next_y;book[next_x][next_y][p1.letter] = true;q.push(p2);}}}return -1;}int main(){int sx,sy;while(scanf("%d%d%d",&n,&m,&t)!=EOF){getchar();for(int i = 0;i<n;i++){for(int j = 0;j<m;j++){scanf("%c",&map[i][j]);if(map[i][j]=='@')sx = i,sy = j;}getchar();}printf("%d\n",bfs(sx,sy));}return 0;}


0 0