| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:t3In Reply To:t3 Posted by:T3 at 2006-02-25 13:27:06 #include <iostream>
#include <queue>
#include <climits>
using namespace std;
#define LEFT 0
#define DOWN 1
#define RIGHT 2
#define UP 3
#define MAX 100
#define VALID(r, c) ((r) >= 0 && (r) < R && (c) >= 0 && (c) < C)
#define VALID2(r, c) (VALID(r, c) && map[r][c] != 'b')
char map[MAX][MAX + 4];
int step[MAX][MAX + 1][4];
int R, C, S, B;
void floodfill(int r, int c)
{
if(VALID(r, c) && map[r][c] == '*')
{
map[r][c] = '#';
floodfill(r + 1, c);
floodfill(r - 1, c);
floodfill(r, c + 1);
floodfill(r, c - 1);
}
}
struct STATE
{
int r, c, e, l;
STATE(void)
{
}
STATE(int _r, int _c, int _e, int _l) : r(_r), c(_c), e(_e), l(_l)
{
}
bool operator <(const STATE &rhs) const
{
return l > rhs.l;
}
};
void solve(void)
{
floodfill(0, 0);
if(map[0][0] != '#' || map[R - 1][C - 1] != '#')
{
cout << "impossible" << endl;
return;
}
int i, j, k;
for(i = 0; i < R; ++i)
{
for(j = 0; j < C; ++j)
{
for(k = 0; k < 4; ++k)
{
step[i][j][k] = INT_MAX;
}
}
}
step[0][0][LEFT] = 0;
step[R - 1][C][LEFT] = INT_MAX;
priority_queue<STATE> q;
q.push(STATE(0, 0, LEFT, 0));
while(!q.empty())
{
STATE x = q.top();
int t, c;
q.pop();
if(x.l > step[x.r][x.c][x.e])
{
continue;
}
if(x.r == R - 1 && x.c == C)
{
cout << x.l << endl;
return;
}
c = x.l;
if(x.r == R - 1 && x.c == C - 1)
{
if(x.e == LEFT)
{
t = c + S;
if(t < step[R - 1][C][LEFT])
{
step[R - 1][C][LEFT] = t;
q.push(STATE(R - 1, C, LEFT, t));
}
}
else
{
t = c + B;
if(t < step[R - 1][C][LEFT])
{
step[R - 1][C][LEFT] = t;
q.push(STATE(R - 1, C, LEFT, t));
}
}
continue;
}
switch(x.e)
{
case LEFT:
{
if(VALID2(x.r + 1, x.c))
{
t = c + B;
if(t < step[x.r + 1][x.c][DOWN])
{
step[x.r + 1][x.c][DOWN] = t;
q.push(STATE(x.r + 1, x.c, DOWN, t));
}
}
if(VALID2(x.r - 1, x.c))
{
t = c + B;
if(t < step[x.r - 1][x.c][UP])
{
step[x.r + 1][x.c][UP] = t;
q.push(STATE(x.r - 1, x.c, UP, t));
}
}
if(VALID2(x.r, x.c + 1))
{
t = c + S;
if(t < step[x.r][x.c + 1][LEFT])
{
step[x.r][x.c + 1][LEFT] = t;
q.push(STATE(x.r, x.c + 1, LEFT, t));
}
}
break;
}
case DOWN:
{
if(VALID2(x.r + 1, x.c))
{
t = c + S;
if(t < step[x.r + 1][x.c][DOWN])
{
step[x.r + 1][x.c][DOWN] = t;
q.push(STATE(x.r + 1, x.c, DOWN, t));
}
}
if(VALID2(x.r, x.c - 1))
{
t = c + B;
if(t < step[x.r][x.c - 1][RIGHT])
{
step[x.r][x.c - 1][RIGHT] = t;
q.push(STATE(x.r, x.c - 1, RIGHT, t));
}
}
if(VALID2(x.r, x.c + 1))
{
t = c + B;
if(t < step[x.r][x.c + 1][LEFT])
{
step[x.r][x.c + 1][LEFT] = t;
q.push(STATE(x.r, x.c + 1, LEFT, t));
}
}
break;
}
case UP:
{
if(VALID2(x.r - 1, x.c))
{
t = c + S;
if(t < step[x.r - 1][x.c][UP])
{
step[x.r + 1][x.c][UP] = t;
q.push(STATE(x.r - 1, x.c, UP, t));
}
}
if(VALID2(x.r, x.c - 1))
{
t = c + B;
if(t < step[x.r][x.c - 1][RIGHT])
{
step[x.r][x.c - 1][RIGHT] = t;
q.push(STATE(x.r, x.c - 1, RIGHT, t));
}
}
if(VALID2(x.r, x.c + 1))
{
t = c + B;
if(t < step[x.r][x.c + 1][LEFT])
{
step[x.r][x.c + 1][LEFT] = t;
q.push(STATE(x.r, x.c + 1, LEFT, t));
}
}
break;
}
case RIGHT:
{
if(VALID2(x.r + 1, x.c))
{
t = c + B;
if(t < step[x.r + 1][x.c][DOWN])
{
step[x.r + 1][x.c][DOWN] = t;
q.push(STATE(x.r + 1, x.c, DOWN, t));
}
}
if(VALID2(x.r, x.c - 1))
{
t = c + S;
if(t < step[x.r][x.c - 1][RIGHT])
{
step[x.r][x.c - 1][RIGHT] = t;
q.push(STATE(x.r, x.c - 1, RIGHT, t));
}
}
if(VALID2(x.r - 1, x.c))
{
t = c + B;
if(t < step[x.r - 1][x.c][UP])
{
step[x.r + 1][x.c][UP] = t;
q.push(STATE(x.r - 1, x.c, UP, t));
}
}
break;
}
}
}
}
int main(void)
{
while(cin >> C >> R >> S >> B)
{
int i;
for(i = R - 1; i >= 0; --i)
{
cin >> map[i];
}
solve();
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator