输出这么蛋疼的一道题我竟然一遍就过了!!!!!!!爽啊!!!!!!!!!!!!!!!!!
AC代码如下:
#include <iostream>
#include <queue>
#include <cstring>
#include <cstdlib>
using namespace std;
typedef struct{
int x, y;
int time;
}Point;
int map[100][100];
int mark[100][100];
int N, M;
int moves[][2] = { 1, 0, -1, 0, 0, 1, 0, -1 };
Point front[100][100];
void myprint( int i, int j, int k, int t ){
if( i != 0 || j != 0 ){
myprint( front[i][j].x, front[i][j].y, i, j );
}
int mm = front[k][t].time + 1;
printf( "%ds:(%d,%d)->(%d,%d)\n", mm, i, j, k, t );
for( int i = 1; i <= map[k][t]; i++ ){
printf( "%ds:FIGHT AT (%d,%d)\n", mm+i, k, t );
}
}
void BFS(){
queue<Point> q;
Point begins;
begins.x = begins.y = begins.time = 0;
mark[0][0] = 0;
q.push( begins );
while( !q.empty() ){
Point p;
p = q.front();
q.pop();
for( int i = 0; i < 4; i++ ){
Point temp = p;
temp.x += moves[i][0];
temp.y += moves[i][1];
if( temp.x >=0 && temp.y >= 0 && temp.x < N && temp.y < M && map[temp.x][temp.y] != -1 ){
temp.time += map[temp.x][temp.y] + 1;
if( temp.time < mark[temp.x][temp.y] ){
mark[temp.x][temp.y] = temp.time;
front[temp.x][temp.y].x = p.x;
front[temp.x][temp.y].y = p.y;
front[temp.x][temp.y].time = p.time;
q.push( temp );
}
}
}
}
if( mark[N-1][M-1] < 999999 ){
cout << "It takes " << mark[N-1][M-1] <<" seconds to reach the target position, let me show you the way." << endl;
myprint( front[N-1][M-1].x, front[N-1][M-1].y, N-1, M-1 );
cout << "FINISH" << endl;
}else{
cout << "God please help our poor hero.\nFINISH" << endl;
}
}
int main(){
char c;
while( scanf( "%d%d", &N, &M ) != EOF ){
memset( map, 0, sizeof( map ) );
getchar();
for( int i = 0; i < N; i++ ){
for( int j = 0; j < M; j++ ){
c = getchar();
if( c == 'X' ){
map[i][j] = -1;
}else if( c >= 49 && c <= 57 ){
map[i][j] = c - 48;
}
mark[i][j] = 999999;
}
getchar();
}
BFS();
}
return 0;
}