1010

===
不知道为什么一直超时我心碎了曹操草,有大神给看看么??

#include<cstdlib>
#include<cstdio>
#include<string.h>
#include<cmath>
#include<iostream>
using namespace std;
struct Point
{
    int x;
    int y;
    int destence;
}start_point,end_point;
int size_x,size_y;
/*struct Direction
{
    int dir_x;
    int dir_y;
}up,left,right,down;
up.dir_x = 0;
up.dir_y = 1;
left.dir_x = -1;
left.dir_y = 0;
right.dir_x = 1;
right.dir_y = 0;
down.dir_x = 0;
down.dir_y = -1;*/
int direction[4][2] = {{0,1},{0,-1},{1,0},{0,1}};// up down left down
char str_map[10][10] = {0};
bool my_find = false;
int my_time;
void DFS(int x,int y,int count)
{
    int road_to_go ;
    if(my_find == true)
        return;
    if(x == end_point.x && y == end_point.y && count == my_time)
    {
        my_find = true;
        return;
    }
    road_to_go = (my_time - count) - (abs(x-end_point.x) + abs(y-end_point.y));
    if(road_to_go % 2 ==1 || road_to_go < 0)
    {
        return;
    }
    for(int i = 0; i < 4; i++)
    {
        if(x + direction[i][0] < 0 || x + direction[i][0] > size_x - 1 || y + direction[i][1] < 0 || y + direction[i][1] > size_y -1  )
        {
            continue;
        }
        if(str_map[x + direction[i][0]][y + direction[i][1]]!= 'X' && str_map[x + direction[i][0]][y + direction[i][1]]!= 'x')
        {
            str_map[x+direction[i][0]][y + direction[i][1]] = 'X';
            DFS(x + direction[i][0], y + direction[i][1],count+1);
            str_map[x+direction[i][0]][y + direction[i][1]] = '.';
        }
        if(my_find == true)
        {
            return;
        }
    }
}
int main()
{
    int i,j;
    int sum_time = 0;
    int wall_count = 0;
    while(scanf("%d %d %d",&size_x,&size_y,&my_time)!=EOF)
    {
        my_find = false;
        sum_time = 0;
        wall_count = 0;
        memset(str_map,0,sizeof(str_map));
        if(size_x == 0 && size_y == 0 && my_time == 0)
            break;
        for(i = 0;i < size_x ;i++)
        {
            for(j = 0; j < size_y;j++)
            {
                cin>>str_map[i][j];
                if(str_map[i][j] == 'S')
                {
                    start_point.x = i;
                    start_point.y = j;

                    str_map[i][j] = 'X';
                }
                if(str_map[i][j] == 'D')
                {
                    end_point.x = i;
                    end_point.y = j;
                    end_point.destence = 0;
                }
                if(str_map[i][j] == 'X')
                    wall_count++;
            }
            //getchar();
        }
        if(size_x*size_y - wall_count >= my_time)
        {
            DFS(start_point.x,start_point.y,0);
        }
        if(my_find == true)
        {
            cout<<"YES"<<endl;
        }
        else
        {
            cout<<"NO"<<endl;
        }
    }
}
posted on 2017-06-07 16:36  HDU李少帅  阅读(142)  评论(0编辑  收藏  举报