码迷,mamicode.com
首页 > 其他好文 > 详细

zoj 月赛

时间:2015-07-26 21:00:22      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:acm   zoj   


Wumpus

Time Limit: 2 Seconds      Memory Limit: 65536 KB

One day Leon finds a very classic game called Wumpus.The game is as follow.
Once an agent fell into a cave. The legend said that in this cave lived a kind of monster called Wumpus, and there were horrible pits which could lead to death everywhere. However, there were also a huge amount of gold in the cave. The agent must be careful and sensitive so that he could grab all of the gold and climb out of the cave safely.

The cave can be regarded as a n*n board. In each square there could be a Wumpus, a pit, a brick of gold, or nothing. The agent would be at position (0,0) at first and headed right.(As the picture below)

技术分享
For each step, there are six possible movements including going forward, turning left, turning right, shooting, grabbing the gold, and climbing out of the cave. If the agent steps into a square containing a pit or Wumpus, he will die. When the agent shoots, the Wumpus in front of him will die. The goal of the agent is to grab all of the gold and return to the starting position and climb out(it‘s OK if any Wumpus is still living).When a brick of gold is grabbed successfully, you will gain 1000 points. For each step you take, you will lose 10 points.

Your job is to help him compute the highest point he can possibly get.

For the purpose of simplification, we suppose that there is only one brick of gold and the agent cannot shoot the Wumpus.

If there is a pit at (0, 0), the agent dies immediately. There will not be a Wumpus at (0, 0).

Input

There are multiple cases. The first line will contain one integer k that indicates the number of cases.

For each case:
The first line will contain one integer n (n <= 20).
The following lines will contain three integers, each line shows a position of an object. The first one indicates the type of the object. 1 for Wumpus, 2 for pit and 3 for gold. Then the next two integers show the x and y coordinates of the object.
The input end with -1 -1 -1. (It is guaranteed that no two things appear in one position.)

Output

The output contains one line with one integer, which is the highest point Leon could possibly get. If he cannot finish the game with a non-negative score, print "-1".

Sample Input

2
3
1 1 1
2 2 0
3 2 2
-1 -1 -1
3
1 1 1
3 2 2
-1 -1 -1

Sample Output

850
870

Hint

For the sample 1, the following steps are taken:
turn left, forward, forward, turn right, forward, forward, grab, turn left, turn left, forward, forward, turn left, forward, forward, climb.
There are in all 15 steps, so the final score is 840. For the sample 2 , the path is as follow:

技术分享


Author: JIANG, Kairong


写超时了 ,先挂上,以后再改

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>

using namespace std;
#define maxn 25
#define INF 0x7ffffff
typedef pair<int, int> P;

int a[maxn][maxn];
int mark[10000][4];
int vis[maxn][maxn];
int n, T;
int flag;
int ans;
int X, Y;

struct Node
{
    int x, y;
    int dre, cost;
};

bool judge(int x, int y)
{
    if(x>=0 && x<n && y>=0 && y<n && a[x][y]!=1 && a[x][y]!=2 && !vis[x][y])
        return true;
    return false;
}

int Min1, Min2;
int go[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; /// up right down left

/// dre = (dre + 1) % 4;  dre = (dre + n - 1) % 4; dre = dre;
void dfs1(int x, int y, int dre, int cost)
{
    if(a[x][y] == 3)
    {
        X = x;
        Y = y;
        if(Min1 >= cost)
        {
            Min1 = cost;
            mark[cost][dre] = 1;
        }
        return ;
    }

    for(int i=-1; i<=2; i++)
    {
        int D = (dre + 4 + i) % 4;
        int xx = x + go[D][0];
        int yy = y + go[D][1];
        if(judge(xx, yy))
            {
                vis[xx][yy] = 1;
                dfs1(xx, yy, D, cost + 10 + abs(i)*10);
                vis[xx][yy] = 0;
            }
    }
}

void dfs2(int x, int y, int dre, int cost)
{
    if(x==0 && y==0)
    {
        Min2 = min(Min2, cost);
        return ;
    }

    for(int i=-1; i<=2; i++)
    {
        int D = (dre + 4 + i) % 4;
        int xx = x + go[D][0];
        int yy = y + go[D][1];
        if(judge(xx, yy))
            {
                vis[xx][yy] = 1;
                dfs2(xx, yy, D, cost + 10 + abs(i)*10);
                vis[xx][yy] = 0;
            }
    }
}

int main()
{
    scanf("%d", &T);
    while(T--)
    {
        X = Y = -1;
        memset(mark, 0, sizeof(mark));
        memset(vis, 0, sizeof(vis));
        memset(a, 0, sizeof(a));
        Min1 = Min2 = INF;
        scanf("%d", &n);
        int t, x, y;
        while(~scanf("%d%d%d", &t, &x, &y) && t!=-1)
            {
                a[x][y] = t;
                if(t == 3)
                {
                    X = x, Y = y;
                }
            }

        if(a[0][0] == 2)
            printf("-1\n");
        else
        {
            vis[0][0] = 1;
            dfs1(0, 0, 1, 0);
            if(X == -1)
                printf("-1\n");
            else
            {
                for(int i=0; i<4; i++)
                    if(mark[Min1][i])
                        {
                            memset(vis, 0, sizeof(vis));
                            vis[X][Y] = 1;
                            dfs2(X, Y, i, 0);
                        }
               int ans = 1000 - Min1 - Min2 - 20;
               printf("%d\n", ans);
            }
        }
    }
    return 0;
}

/*

5

4
2 0 0
-1 -1 -1

3
1 0 1
2 1 0
-1 -1 -1

4
3 0 0
1 2 2
-1 -1 -1

3
1 1 0
2 1 1
3 2 0
-1 -1 -1

3
1 0 1
2 1 1
3 0 2
-1 -1 -1



*/


版权声明:本文为博主原创文章,未经博主允许不得转载。

zoj 月赛

标签:acm   zoj   

原文地址:http://blog.csdn.net/dojintian/article/details/47070809

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!