| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4876 人关注过本帖
标题:一道acm的搜索题目,昨天搞了一下午一直WA,实在找不到错误,测试数据通过
只看楼主 加入收藏
ffbh
Rank: 2
等 级:论坛游民
威 望:1
帖 子:27
专家分:10
注 册:2015-1-15
结帖率:100%
收藏
 问题点数:0 回复次数:4 
一道acm的搜索题目,昨天搞了一下午一直WA,实在找不到错误,测试数据通过
The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now he gets into feng5166's castle. The castle is a large labyrinth. To make the problem simply, we assume the labyrinth is a N*M two-dimensional array which left-top corner is (0,0) and right-bottom corner is (N-1,M-1). Ignatius enters at (0,0), and the door to feng5166's room is at (N-1,M-1), that is our target. There are some monsters in the castle, if Ignatius meet them, he has to kill them. Here is some rules:

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.
 

Input
The input contains several test cases. Each test case starts with a line contains two numbers N and M(2<=N<=100,2<=M<=100) which indicate the size of the labyrinth. Then a N*M two-dimensional array follows, which describe the whole labyrinth. The input is terminated by the end of file. More details in the Sample Input.
 

Output
For each test case, you should output "God please help our poor hero." if Ignatius can't reach the target position, or you should output "It takes n seconds to reach the target position, let me show you the way."(n is the minimum seconds), and tell our hero the whole path. Output a line contains "FINISH" after each test case. If there are more than one path, any one is OK in this problem. More details in the Sample Output.
 

Sample Input

5 6
.XX.1.
..X.2.
2...X.
...XX.
XXXXX.
5 6
.XX.1.
..X.2.
2...X.
...XX.
XXXXX1
5 6
.XX...
..XX1.
2...X.
...XX.
XXXXX.

 

Sample Output

It takes 13 seconds to reach the target position, let me show you the way.
1s:(0,0)->(1,0)
2s:(1,0)->(1,1)
3s:(1,1)->(2,1)
4s:(2,1)->(2,2)
5s:(2,2)->(2,3)
6s:(2,3)->(1,3)
7s:(1,3)->(1,4)
8s:FIGHT AT (1,4)
9s:FIGHT AT (1,4)
10s:(1,4)->(1,5)
11s:(1,5)->(2,5)
12s:(2,5)->(3,5)
13s:(3,5)->(4,5)
FINISH
It takes 14 seconds to reach the target position, let me show you the way.
1s:(0,0)->(1,0)
2s:(1,0)->(1,1)
3s:(1,1)->(2,1)
4s:(2,1)->(2,2)
5s:(2,2)->(2,3)
6s:(2,3)->(1,3)
7s:(1,3)->(1,4)
8s:FIGHT AT (1,4)
9s:FIGHT AT (1,4)
10s:(1,4)->(1,5)
11s:(1,5)->(2,5)
12s:(2,5)->(3,5)
13s:(3,5)->(4,5)
14s:FIGHT AT (4,5)
FINISH
God please help our poor hero.
FINISH





下面是我的代码:
#include <iostream>
#include <string>
#include <queue>
#include <fstream>
#include <stack>
using namespace std;
struct node{
    int x, y;//保存改node的坐标,输出时用的
    int fight;//代表与怪物的战斗秒数
    bool visited;//表示此node是否被访问过,1代表访问过
    node* pre;//指向此node的前驱node, 因为输出时要输出最短路径
    int g, h, f;//g代表走的step,h代表当前node与终点的距离(忽略障碍物),f=g+h
    bool operator <(const node& kk)const{//优先队列插入时的比较函数
        return f > kk.f;
    }
};
struct Point{
    int x, y;
};
int n, m;
node map[111][111];//地图

priority_queue<node> qk;//保存还没访问过的node
stack<Point> sp;//输出时用的

bool in(const node& kk){//判断node是否越界
    return !(kk.x < 0 || kk.x >= n || kk.y < 0 || kk.y >= m);
}

int manha(const node& kk){//计算当前节点与终点的距离(忽略障碍物)
    return n - 1 - kk.x + m - 1 - kk.y;
}
int dirs[4][2] = { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 } };//可以走的四个方向

int min(int a, int b){
    return a < b ? a : b;
}
bool Astar(){
    node t, s;
    map[0][0].pre = 0;
    bool flag = 0;

    while (!qk.empty()){
        t = qk.top();
        qk.pop();
        map[t.x][t.y].visited = 1;
        for (int i = 0; i < 4; ++i){
            s.x = t.x + dirs[i][0];
            s.y = t.y + dirs[i][1];
            if (in(s) && !map[s.x][s.y].visited){
                s.g = t.g + 1;
                s.h = manha(s);
                s.f = s.g + s.h + map[s.x][s.y].fight;
                if (s.f <= map[s.x][s.y].f){
                    map[s.x][s.y].pre = &map[t.x][t.y];
                    map[s.x][s.y].f = s.f;
                }
                if (s.x == n - 1 && s.y == m - 1)
                    flag = 1;

                qk.push(s);
            }

        }


    }
    return flag;

}


void show(){
    while (!sp.empty()){
        Point old = sp.top();
        sp.pop();
        Point now = old;
        int time = 1;
        while (!sp.empty()){
            old = now;
            now = sp.top();
            sp.pop();
            if (map[old.x][old.y].fight == 0)
                printf("%ds:(%d,%d)->(%d,%d)\n", time++, old.x, old.y, now.x, now.y);
            else{
                while (map[old.x][old.y].fight--)
                    printf("%ds:FIGHT AT (%d,%d)\n", time++, old.x, old.y);
                printf("%ds:(%d,%d)->(%d,%d)\n", time++, old.x, old.y, now.x, now.y);
            }

        }
        while (map[n - 1][m - 1].fight--)
            printf("%ds:FIGHT AT (%d,%d)\n", time++, n - 1, m - 1);


    }

}

int main(){
    string ss;
    node ok;
    while (cin >> n >> m){
        int i = 0;
        memset(map, 0, 111 * 111 * sizeof(node));
        while (i < n){
            cin >> ss;
            for (int j = 0; j < m; ++j){
                if (ss[j] == '.'){
                    map[i][j].x = i;
                    map[i][j].y = j;
                    map[i][j].visited = 0;
                    map[i][j].fight = 0;
                    map[i][j].f = 9000000;
                    map[i][j].g = 0;
                }
                else if (ss[j] == 'X'){
                    map[i][j].x = i;
                    map[i][j].y = j;
                    map[i][j].visited = 1;
                    map[i][j].f = 9000000;
                    map[i][j].g = 0;
                }
                else{
                    map[i][j].x = i;
                    map[i][j].y = j;
                    map[i][j].fight = ss[j] - '0';
                    map[i][j].visited = 0;
                    map[i][j].f = 90000000;
                    map[i][j].g = 0;
                }
            }
            ++i;
        }
        while (!qk.empty()) qk.pop();
        while (!sp.empty()) sp.pop();
        ok.x = ok.y = 0;
        ok.g = 0;
        ok.h = manha(ok);
        ok.f = ok.g + ok.h;
        qk.push(ok);
        if (Astar())
        {
            int sum = 0;
            Point p;
            p.x = n - 1;
            p.y = m - 1;
            node* temp = &map[n - 1][m - 1];
            while (temp->pre){
                p.x = temp->x;
                p.y = temp->y;
                sp.push(p);
                sum += temp->fight + 1;
                temp = temp->pre;
            }
            p.x = temp->x;
            p.y = temp->y;
            sp.push(p);
            printf("It takes %d seconds to reach the target position, let me show you the way.\n", sum);
            show();
        }
        else
            printf("God please help our poor hero.\n");
        printf("FINISH\n");
    }


    return 0;
}
搜索更多相关主题的帖子: problem castle pretty target 
2015-11-07 15:19
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:0 
建议建立一个整型二维数组,全部初始化为一个很大的值。起点的值赋为step=0.从起点位置有可能可以往右或下移动一步。如果无法移动,遍历到下一个可能可通行的位置。如果可移动,就将step+1的值和整型二维数组对应位置上的值作对比(如果新位置有怪,则用step+n去比较)。当step+1较小时,将step+1赋给该整型变量,并在新位置进行递归运算。如果step+1较大或两者一样大,表明之前已有更快或同样快的方式到达新位置,则放弃赋值并不再递归。这样一直让它递归下去,当遇上[N-1][M-1]时递归结束,输出该位置上的整型值,就是最短时间。如果输出了初始化时的极大值,表明该终点不可到达。
程序应该不难,不过最近没有时间写。你自己慢慢研究吧。
2016-01-07 12:38
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:0 
我好像说错了,遇到终点还不能结束递归,因为这时只能说明地图能走得通。要将所有行走的可能遍历完了才能更新出最短用时。
2016-01-07 12:44
ffbh
Rank: 2
等 级:论坛游民
威 望:1
帖 子:27
专家分:10
注 册:2015-1-15
收藏
得分:0 
回复 3楼 yangfrancis
回来看看
2019-01-25 17:50
ffbh
Rank: 2
等 级:论坛游民
威 望:1
帖 子:27
专家分:10
注 册:2015-1-15
收藏
得分:0 
回复 3楼 yangfrancis
最后好像用Astar做的,太暴力会超时
2019-01-25 17:51
快速回复:一道acm的搜索题目,昨天搞了一下午一直WA,实在找不到错误,测试数据 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028020 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved