首页 > ACM题库 > HDU-杭电 > hdu 2213 Save XX-动态规划-[解题报告]C++
2014
01-04

hdu 2213 Save XX-动态规划-[解题报告]C++

Save XX

问题描述 :

Once a day, XX has been caught by evil Z. WisKey, our team leader, has to rescue her. They have escaped from Z’s castle. But unfortunately, they lost in the forest. What’s more, this forest is a big maze. There is only one place to leave. Z is familiar with the forest, and he will arrive at the place in T minutes.
To make the problem simply, we assume the forest is a N*M two-dimensional array which left-top corner is (0,0) and right-bottom corner is (N-1,M-1). The place they stayed now is marked S , and the place to leave is marked E. They could only move to empty place in four directions (up, down , left, right ), or stay at the place.
>  . : empty
>  X: the tree
>  S: the place Wiskey and XX now stay
>  E: the place to leave
Your task is to give out whether they can escape.

输入:

The input contains several test cases. Each test case starts with a line contains three number N ,M (2<= N <= 100, 2 <= M <= 100 ) indicate the size of the maze, T (2 <= T <= 20) indicate the time Z cost to arrive at E place. Then N lines follows, each line contains M character. A S and a E will be in the map.

输出:

The input contains several test cases. Each test case starts with a line contains three number N ,M (2<= N <= 100, 2 <= M <= 100 ) indicate the size of the maze, T (2 <= T <= 20) indicate the time Z cost to arrive at E place. Then N lines follows, each line contains M character. A S and a E will be in the map.

样例输入:

5 6 4
XXXX..
XS.X..
XX.XXX
XX..EX
XXXXXX
5 6 5
XXXX..
XS.X..
XX..XX
XX..EX
XXXXXX
5 6 6
XXXX..
XS.X..
XX..XX
XX..EX
XXXXXX

样例输出:

God will bless XX and WisKey!
Oh, my god, bad luck!
2

OJ

#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>

using namespace std;
#define N 200
typedef __int64 LL;

int n,m,t;
char maps[N][N];
LL dp[22][N][N];
int dist[4][2] = {0,1,0,-1,1,0,-1,0};
struct P
{
       int x,y;
};
P start;
P ens;

bool is(int x,int y)
{
     if(x<0||y<0||x>=n||y>=m)
     return false;
     return true;
 }
void solve()
{
     memset(dp,0,sizeof(dp));
     dp[0][start.x][start.y]= 1;
     
     for(int i=1;i<=t;i++)
     {
             for(int j=0;j<n;j++)
             for(int k =0;k<m;k++)
             {     dp[i][j][k] += dp[i-1][j][k];
                    if(maps[j][k] != 'X' ||maps[j][k]== 0)
                     for(int x=0;x<4;x++)
                     {     
                             int xx,y;
                             xx = j+dist[x][0];
                             y = k+dist[x][1];
                             if(is(xx,y))
                             dp[i][j][k] += dp[i-1][xx][y];
                     }
             }
     }
    LL as =  dp[t-1][ens.x][ens.y];
    LL ab = dp[t][ens.x][ens.y];
    if(as>0) printf("%I64d\n",as);
    else if(ab>0) puts("Oh, my god, bad luck!");
    else puts("God will bless XX and WisKey!");
 }
void init()
{
     while(scanf("%d%d%d",&n,&m,&t)!=EOF)
     {
         for(int i=0;i<n;i++)
           scanf("%s",maps[i]);
         
         for(int i=0;i<n;i++)
         for(int j = 0;j<m;j++)
         if(maps[i][j] == 'S') start.x = i,start.y = j;
         else if(maps[i][j] == 'E') ens.x = i,ens.y = j;
         solve();
         
     }
 }
int main()
{
    init();
    return 0;
    }

解题转自:http://blog.csdn.net/gongqian12345/article/details/7738903


  1. Good task for the group. Hold it up for every yeara??s winner. This is a excellent oppotunity for a lot more enhancement. Indeed, obtaining far better and much better is constantly the crucial. Just like my pal suggests on the truth about ab muscles, he just keeps obtaining much better.