hdu 1885 BFS + 二进制压缩

来源:互联网 发布:mac口红中国专柜价格 编辑:程序博客网 时间:2024/05/23 12:45
#include <iostream>#include <queue>#include <cstdio>#include <cstring>using namespace std;typedef struct{int x, y;int state;}Point;char map[150][150];int moves[][2] = { 0, 1, 0, -1, 1, 0, -1, 0 };int step[1<<7][150][150];int N, M, T;Point start, last;int ans;void BFS(){queue<Point> q;start.state = 0;step[0][start.x][start.y] = 0;q.push( start );while( !q.empty() ){Point p = q.front();q.pop();if( map[p.x][p.y] == 'X' ){if( step[p.state][p.x][p.y] < ans ){ans = step[p.state][p.x][p.y];}}for( int i = 0; i < 4; i++ ){Point temp;temp.x = p.x + moves[i][0];temp.y = p.y + moves[i][1];if( temp.x < 0 || temp.x >= N || temp.y < 0 || temp.y >= M || map[temp.x][temp.y] == '#' ){continue;}if( map[temp.x][temp.y] == 'R' || map[temp.x][temp.y] == 'G' || map[temp.x][temp.y] == 'B' || map[temp.x][temp.y] == 'Y' ){int s;if( map[temp.x][temp.y] == 'B' ){s = 1;}else if( map[temp.x][temp.y] == 'Y' ){s = 2;}else if( map[temp.x][temp.y] == 'R' ){s = 3;}else if( map[temp.x][temp.y] == 'G' ){s = 4;}if( ( p.state & ( 1 << ( s ) ) ) == 0 ){continue;}}if( step[p.state][temp.x][temp.y] != -1 ){continue;}if( map[temp.x][temp.y] == 'r' || map[temp.x][temp.y] == 'g' || map[temp.x][temp.y] == 'b' || map[temp.x][temp.y] == 'y' ){int s;if( map[temp.x][temp.y] == 'b' ){s = 1;}else if( map[temp.x][temp.y] == 'y' ){s = 2;}else if( map[temp.x][temp.y] == 'r' ){s = 3;}else if( map[temp.x][temp.y] == 'g' ){s = 4;}temp.state = ( p.state | 1 << s );}else{temp.state = p.state;}step[temp.state][temp.x][temp.y] = step[p.state][p.x][p.y] + 1;q.push( temp );}}}int main(){while( scanf( "%d%d", &N, &M ) && !( N == 0 && M == 0 ) ){getchar();for( int i = 0; i < N; i++ ){for( int j = 0; j < M; j++ ){char c = getchar();if( c == '*' ){start.x = i;start.y = j;}map[i][j] = c;}getchar();}memset( step, -1, sizeof( step ) );ans = 999999;BFS();if( ans != 999999 ){printf( "Escape possible in %d steps.\n", ans );}else{cout << "The poor student is trapped!" << endl;}}return 0;}

原创粉丝点击