| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 559 人关注过本帖
标题:一道acm的搜索题目,昨天搞了一下午一直WA,实在找不到错误,测试数据通过
只看楼主 加入收藏
ffbh
Rank: 2
等 级:论坛游民
威 望:1
帖 子:27
专家分:10
注 册:2015-1-15
结帖率:100%
收藏
 问题点数:0 回复次数:3 
一道acm的搜索题目,昨天搞了一下午一直WA,实在找不到错误,测试数据通过
我大概的说下这题目的意思:就是先输入两个int型的数据,n,m,分别代表长和宽,然后紧跟着n行字符串,每个字符串长度为m,代表地图,'.'代表空地,'X'代表障碍物
数字((1<=i<=9))代表怪物的数量,每个怪物要消耗1秒的时间才能消灭,走一格要一秒时间,然后就是求从坐标(0,0)到坐标(n-1,m-1)的最短时间并且输出,如果没有的话就输出”God please help our poor hero.“具体下面有输入和输出的范例

这题目困扰了我好久了,实在找不到错误,发在c++论坛没人回,,,c论坛人比较多,就发过来了,,,,,,用c的大大们不要介意啊


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.


输出范例
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.



输入范例

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;
}
搜索更多相关主题的帖子: please 字符串 怪物 please 字符串 怪物 
2015-11-07 16:21
ffbh
Rank: 2
等 级:论坛游民
威 望:1
帖 子:27
专家分:10
注 册:2015-1-15
收藏
得分:0 
写错了,上面是输入范例,下面是输出的
2015-11-07 16:22
天色
Rank: 1
来 自:东北大学
等 级:新手上路
帖 子:5
专家分:0
注 册:2015-12-14
收藏
得分:0 
好长
2015-12-17 19:24
ffbh
Rank: 2
等 级:论坛游民
威 望:1
帖 子:27
专家分:10
注 册:2015-1-15
收藏
得分:0 
回复 3楼 天色
回来看看
2019-01-25 17:52
快速回复:一道acm的搜索题目,昨天搞了一下午一直WA,实在找不到错误,测试数据 ...
数据加载中...
 
   



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

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