Ignatius and the Princess I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16500 Accepted Submission(s): 5248
Special Judge
1.Ignatius can only move in four directions(up, down, left, right), one step per second. A step is defined as follow: if current position is (x,y), after a step, Ignatius can only stand on (x-1,y), (x+1,y), (x,y-1) or (x,y+1).
2.The array is marked with some characters and numbers. We define them like this:
. : The place where Ignatius can walk on.
X : The place is a trap, Ignatius should not walk on it.
n : Here is a monster with n HP(1<=n<=9), if Ignatius walk on it, it takes him n seconds to kill the monster.
Your task is to give out the path which costs minimum seconds for Ignatius to reach target position. You may assume that the start position and the target position will never be a trap, and there will never be a monster at the start position.
//************************************************************************//
//*Author : Handsome How *//
//************************************************************************//
//#pragma comment(linker, "/STA CK:1024000000,1024000000")
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
#include <ctime>
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) __typeof(v) r = (v)
#endif
#define foreach(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define fur(i,a,b) for(int i=(a);i<=(b);i++)
#define furr(i,a,b) for(int i=(a);i>=(b);i--)
#define cl(a) memset((a),0,sizeof(a))
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define dbg(x) cout << #x << " = " << x << endl
#ifdef HandsomeHow
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> pii;
const int inf=0x3f3f3f3f;
const double eps=1e-8;
const int mod=1000000007;
const double pi=acos(-1);
inline void gn(long long&x){
int sg=1;char c;while(((c=getchar())<'0'||c>'9')&&c!='-');c=='-'?(sg=-1,x=0):(x=c-'0');
while((c=getchar())>='0'&&c<='9')x=x*10+c-'0';x*=sg;
}
inline void gn(int&x){long long t;gn(t);x=t;}
inline void gn(unsigned long long&x){long long t;gn(t);x=t;}
int gcd(int a,int b){return a? gcd(b%a,a):b;}
ll powmod(ll a,ll x,ll mod){ll t=1ll;while(x){if(x&1)t=t*a%mod;a=a*a%mod;x>>=1;}return t;}
// (づ°ω°)づe★
//------------------------------------------------
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};
const int maxn = 105;
char mp[maxn][maxn];
bool vis[maxn][maxn];
int X,Y;
struct Node{
int x;
int y;
int time;
vector<pii>v;
friend bool operator<(Node a, Node b){
return a.time > b.time ;
}
};
bool check(int x, int y){
return x>=0&&y>=0&&x<X&&y<Y&&!vis[x][y]&&mp[x][y]!='X';
}
Node bfs(){
vis[0][0]=true;
Node now,next;
now.x = 0;
now.y = 0;
now.time = 0;
now.v.push_back(make_pair(0,0));
priority_queue<Node>q;
q.push(now);
while(!q.empty()){
now = q.top();
if(now.x == X-1 && now.y == Y -1) return now;
q.pop();
next = now;
fur(i,0,3){
next.x = now.x + dx[i];
next.y = now.y + dy[i];
if(!check(next.x,next.y))continue;
if(mp[next.x][next.y]=='.') next.time = now.time + 1;
else next.time = now.time + mp[next.x][next.y]-'0' + 1;
next.v.push_back(make_pair(next.x,next.y));
vis[next.x][next.y] = true;
q.push(next);
next.v.pop_back();
}
}
Node tmp;
tmp.time = -1;
return tmp;
}
int main(){
#ifdef HandsomeHow
//freopen("E:\\data.in","r",stdin);
//freopen("E:\\data.out","w",stdout);
time_t beginttt = clock();
#endif
while(scanf("%d %d",&X, &Y) != EOF){
cl(vis);
fur(i,0,X-1)scanf("%s",mp[i]);
Node ans;
if(mp[0][0] == 'X')
ans.time = -1;
else
ans = bfs();
if(ans.time == -1){
printf("God please help our poor hero.\nFINISH\n");
}
else{
printf("It takes %d seconds to reach the target position, let me show you the way.\n",ans.time);
int SZ = ans.v.size();
pii now;
int t = 1;
now = ans.v[0];
if(mp[0][0]!='.'){
for(int k = 0; k < mp[now.first][now.second] - '0'; ++k){
printf("%ds:FIGHT AT (%d,%d)\n",t++,now.first,now.second);
}
}
for(int i = 1; i < SZ; ++i){
printf("%ds:(%d,%d)->(%d,%d)\n",t++,now.first,now.second,ans.v[i].first,ans.v[i].second);
now = ans.v[i];
if(mp[now.first][now.second]!='.'){
for(int k = 0; k < mp[now.first][now.second] - '0'; ++k){
printf("%ds:FIGHT AT (%d,%d)\n",t++,now.first,now.second);
}
}
}
printf("FINISH\n");
}
}
#ifdef HandsomeHow
time_t endttt = clock();
debug("time: %d\n",(int)(endttt - beginttt));
#endif
return 0;
}