Ignatius and the Princess I hdu 1026 priority_queue + bfs (vector)
来源:互联网 发布:唐七公子 知乎 编辑:程序博客网 时间:2024/05/18 09:03
/*
崩溃到极点的priotity_queue + bfs,关键是路径的保存这点,网上看了说是要有个前驱
可是后来自己的写的时候就是不知怎么保存好,后来用了类试yt的方法,但是还是好久都没有做了
当在cfree在过了测试数据的时候,结果交上去是CE,using namespace std;这个忘记了加
加上去了是wrong 15ms ,后来发现时count1 没有初始话
*/
#include<iostream>//2486536 2010-05-24 21:00:33 Accepted 1026 31MS 464K 2482 B C++ 悔惜晟
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
int x;
int y;
int t;
int flag;
int pre;
friend bool operator <(node a, node b)
{
return a.t > b.t;
}
}df[10000], dff[10000];
/*
struct path
{
int x;
int y;
int flag;
int pre;
}df[10000], dff[10000];
*/
char map[105][105];
bool hash[105][105];
int dir[4][2] = { {0, 1}, {0, -1}, {-1, 0}, {1, 0} };
int n, m;
int count1;
int bfs()
{
priority_queue<node> Q;
node N, P;
int i;
count1 = 0;
N.x = 0;
N.y = 0;
N.t = 0;
N.flag = 0;
N.pre = -1;
Q.push(N);
while(!Q.empty())
{
N = Q.top();
df[count1] = N;
if(N.x == n - 1 && N.y == m - 1)
{
return N.t;
}
Q.pop();
for(i = 0 ; i < 4; i++)
{
P.x = N.x + dir[i][0];
P.y = N.y + dir[i][1];
P.pre = count1;
if(P.x >= 0 && P.x < n && P.y >= 0 && P.y < m && !hash[P.x][P.y] && map[P.x][P.y] != 'X')
{
if(map[P.x][P.y] == '.')
{
P.t = N.t + 1;
P.flag = 0;
Q.push(P);
}
else
{
P.t = N.t + 1 + map[P.x][P.y] - '0';
P.flag = map[P.x][P.y] - '0';
Q.push(P);
}
hash[P.x][P.y] = true;
}
}
count1++;
}
return -1;
}
int main()
{
while(cin>>n>>m)
{
int i, j, tt;
for(i = 0; i < n ; i++)
for(j = 0; j < m ; j++)
cin>>map[i][j];
memset(hash, false, sizeof(hash));
hash[0][0] = true;
tt = bfs();
int c = tt;
if(tt == -1)
{
printf("God please help our poor hero./nFINISH/n");
continue;
}
else
{
printf("It takes %d seconds to reach the target position, let me show you the way./n", tt);
int w = count1;
while(c >= 0)
{
if(df[w].flag == 0)
{
dff[c--] = df[w];
w = df[w].pre;
}
else
{
dff[c--] = df[w];
for(i = 0; i < df[w].flag; i++)
{
dff[c--] = df[w];
}
w = df[w].pre;
}
}
for(i = 1; i <= tt ;)
{
printf("%ds:(%d,%d)->(%d,%d)/n", i , dff[i - 1].x, dff[ i - 1].y, dff[i].x, dff[i].y);
if(map[ dff[i].x ][ dff[i].y ] >= '1' && map[ dff[i].x ][ dff[i].y ] <= '9')
{
for(j = 1; j <= dff[i].flag; j++)
printf("%ds:FIGHT AT (%d,%d)/n", i + j, dff[i].x, dff[i].y);
i = i + j;
}
else
i++;
}
printf("FINISH/n");
}
}
}
/*
崩溃到极点的priotity_queue + bfs,关键是路径的保存这点,网上看了说是要有个前驱
可是后来自己的写的时候就是不知怎么保存好,后来用了类试yt的方法,但是还是好久都没有做了
当在cfree在过了测试数据的时候,结果交上去是CE,using namespace std;这个忘记了加
加上去了是wrong 15ms ,后来发现时count1 没有初始话
Memory Limit Exceeded
*/
#include<iostream>//2486536 2010-05-24 21:00:33 Accepted 1026 31MS 464K 2482 B C++ 悔惜晟
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
int x;
int y;
int t;
int flag;
int pre;
friend bool operator <(node a, node b)
{
return a.t > b.t;
}
}T;//df[10000], dff[10000];
/*
struct path
{
int x;
int y;
int flag;
int pre;
}df[10000], dff[10000];
*/
vector<node> df, dff;
char map[105][105];
bool hash[105][105];
int dir[4][2] = { {0, 1}, {0, -1}, {-1, 0}, {1, 0} };
int n, m;
int count1;
int bfs()
{
priority_queue<node> Q;
node N, P;
int i;
df.clear();//加了这个就AC了2487014 2010-05-24 22:39:39 Accepted 1026 31MS 552K 3148 B C++ 悔惜晟
dff.clear();
count1 = 0;
N.x = 0;
N.y = 0;
N.t = 0;
N.flag = 0;
N.pre = -1;
Q.push(N);
while(!Q.empty())
{
N = Q.top();
//df[count1] = N;
df.push_back(N);
if(N.x == n - 1 && N.y == m - 1)
{
return N.t;
}
Q.pop();
for(i = 0 ; i < 4; i++)
{
P.x = N.x + dir[i][0];
P.y = N.y + dir[i][1];
P.pre = count1;
if(P.x >= 0 && P.x < n && P.y >= 0 && P.y < m && !hash[P.x][P.y] && map[P.x][P.y] != 'X')
{
if(map[P.x][P.y] == '.')
{
P.t = N.t + 1;
P.flag = 0;
Q.push(P);
}
else
{
P.t = N.t + 1 + map[P.x][P.y] - '0';
P.flag = map[P.x][P.y] - '0';
Q.push(P);
}
hash[P.x][P.y] = true;
}
}
count1++;
}
return -1;
}
int main()
{
while(cin>>n>>m)
{
int i, j, tt, ee;
for(i = 0; i < n ; i++)
for(j = 0; j < m ; j++)
cin>>map[i][j];
memset(hash, false, sizeof(hash));
hash[0][0] = true;
tt = bfs();
int c = tt;
if(tt == -1)
{
printf("God please help our poor hero./nFINISH/n");
continue;
}
else
{
printf("It takes %d seconds to reach the target position, let me show you the way./n", tt);
int w = df.size() - 1;
while(c >= 0)
{
if(df[w].flag == 0)
{
//dff[c--] = df[w];
T = df[w];
//df.pop_back();
dff.push_back(T);
c--;
w = T.pre;
}
else
{
T = df[w];
dff.push_back(T);
c--;
for(i = 0; i < df[w].flag; i++)
{
dff.push_back(T);
}
c -= df[w].flag;
df.pop_back();
w = T.pre;
}
}
ee = 1;
for(i = tt - 1; i >= 0 ;)
{
printf("%ds:(%d,%d)->(%d,%d)/n", ee++ , dff[i + 1].x, dff[ i + 1].y, dff[i].x, dff[i].y);
if(map[ dff[i].x ][ dff[i].y ] >= '1' && map[ dff[i].x ][ dff[i].y ] <= '9')
{
for(j = 1; j <= dff[i].flag; j++)
printf("%ds:FIGHT AT (%d,%d)/n", ee++, dff[i].x, dff[i].y);
i = i - j ;
}
else
i--;
}
printf("FINISH/n");
}
}
}
- Ignatius and the Princess I hdu 1026 priority_queue + bfs (vector)
- hdu 1026 Ignatius and the Princess I(bfs+priority_queue)
- hdu 1026 Ignatius and the Princess I (BFS)
- HDU-1026 Ignatius and the Princess I (BFS)
- hdu 1026 Ignatius and the Princess I BFS+剪枝
- HDU 1026 Ignatius and the Princess I && BFS+记录路径
- hdu 1026 Ignatius and the Princess I bfs+优先级队列
- hdu 1026 Ignatius and the Princess I(BFS)
- hdu 1026 Ignatius and the Princess I----bfs+递归
- Hdu 1026 bfs Ignatius and the Princess I
- hdu 1026 Ignatius and the Princess I(优先队列+BFS)
- hdu 1026 Ignatius and the Princess I BFS
- hdu 1026 Ignatius and the Princess I 优先队列 + bfs
- HDU 1026 Ignatius and the Princess I - BFS + 优先队列
- hdu 1026 Ignatius and the Princess I(bfs)
- HDU 1026 Ignatius and the Princess I BFS打印路径
- 【Bfs】HDU 1026 Ignatius and the Princess I
- hdu 1026 Ignatius and the Princess I(BFS)
- CArray的用法
- pku2195二分图的最佳匹配问题mub
- as3 sound 类及相关类
- .NET面试题及答案2
- Javascriot window对象
- Ignatius and the Princess I hdu 1026 priority_queue + bfs (vector)
- TCP三次握手协议的简单解释
- 老牌ORM框架:iBatis更名为MyBatis
- 新的开始。
- Google说,我们不使用C++异常
- 吐槽csdn
- Eclipse命令行参数大全
- 使用Eclipse插件提高代码的质量
- C++入门解惑——浅析cout