【hdu】The Donkey of Gui Zhou(搜索)

一开始的思路是,先走驴子,之后走老虎,每次走的时候记录他们的达到该位置的步数,如果一样就可以相遇,特殊处理末尾。

但是没办法过,之后索性让他们一起走,一起修改移动的坐标,结果过了。。。

真心不知道错哪了。。

11662502 2014-09-16 00:43:01 Accepted 4740 31MS 8300K 2547 B G++ KinderRiven
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<set>
#include<algorithm>
#include<map>
#include<sstream>
#include<cmath>
#include<queue>
#define INF (1 << 30)
#define eps (1e-10)
#define _PI acos(-1.0)
using namespace std;
/*=========================================
=========================================*/
#define MAXD 1000 + 10
int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}}; //¶« ÄÏ Î÷ ±±
int n;
int dist1[MAXD][MAXD];
int dist2[MAXD][MAXD];
int ans_x,ans_y;
void dfs_d(int x,int y,int d,int dist){
    dist1[x][y] = dist;
    int _x = x;
    int _y = y;
    int _dist = dist;
    while(true){
        int t_x = _x;
        int t_y = _y;
        _x = _x + dir[d][0];
        _y = _y + dir[d][1];
        if(_x >= 0 && _x < n && _y >=0 && _y < n && dist1[_x][_y] == -1){
            _dist ++;
            dist1[_x][_y] = _dist;
        }
        else{
            _x = t_x;
            _y = t_y;
            d = (d + 1) % 4;
            _x = _x + dir[d][0];
            _y = _y + dir[d][1];
            if(_x >= 0 && _x < n && _y >= 0 && _y < n && dist1[_x][_y] == -1){
                dfs_d(_x,_y,d,_dist + 1);
                return ;
            }
            else{
                return ;
            }
        }
    }
}
void dfs_t(int x,int y,int d,int dist){
    dist2[x][y] = dist;
    int _x = x;
    int _y = y;
    int _dist = dist;
    while(true){
        int t_x = _x;
        int t_y = _y;
        _x = _x + dir[d][0];
        _y = _y + dir[d][1];
        if(_x >= 0 && _x < n && _y >=0 && _y < n && dist2[_x][_y] == -1){
            _dist ++;
            dist2[_x][_y] = _dist;
        }
        else{
            _x = t_x;
            _y = t_y;
            d = (d + 3) % 4;
            _x = _x + dir[d][0];
            _y = _y + dir[d][1];
            if(_x >= 0 && _x < n && _y >= 0 && _y < n && dist2[_x][_y] == -1){
                dfs_t(_x,_y,d,_dist + 1);
                return ;
            }
            else{
                return ;
            }
        }
    }
}
int main(){
    while(scanf("%d",&n) && n){
        int x , y, d;
        memset(dist1,-1,sizeof(dist1));
        memset(dist2,-1,sizeof(dist2));
        scanf("%d%d%d",&x,&y,&d);
        dfs_d(x,y,d,0);
        scanf("%d%d%d",&x,&y,&d);
        dfs_t(x,y,d,0);
        int ans_dist = INF;
        for(int i = 0 ; i < n ; i++){
            for(int j = 0 ; j < n ; j++)
                printf("%d ",dist1[i][j]);
            printf("\n");
        }
        for(int i = 0 ; i < n ; i++)
        for(int j = 0 ; j < n ; j++)if(dist1[i][j] != -1 && dist2[i][j] != -1){
                if(dist1[i][j] == dist2[i][j]){
                    if(ans_dist > dist1[i][j]){
                        ans_x = i;
                        ans_y = j;
                        ans_dist  = dist1[i][j];
                    }
                }
            }
        if(ans_dist < INF)
            printf("%d %d\n",ans_x,ans_y);
        else
            printf("-1\n");
    }
    return 0;
}
时间: 2024-10-26 13:51:15

【hdu】The Donkey of Gui Zhou(搜索)的相关文章

hdu 4740 The Donkey of Gui Zhou bfs

The Donkey of Gui Zhou Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4740 Description There was no donkey in the province of Gui Zhou, China. A trouble maker shipped one and put it in the forest which could be

HDU 4740 The Donkey of Gui Zhou

The Donkey of Gui Zhou Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 474064-bit integer IO format: %I64d      Java class name: Main There was no donkey in the province of Gui Zhou, China. A trouble maker sh

hdu 4740 The Donkey of Gui Zhou(dfs模拟好题)

Problem Description There was no donkey in the province of Gui Zhou, China. A trouble maker shipped one and put it in the forest which could be considered as an N×N grid. The coordinates of the up-left cell is (0,0) , the down-right cell is (N-1,N-1)

HDU 1312 ----- Red and Black 入门搜索 DFS解法

HDU 1312 ----- Red and Black  入门搜索  http://acm.hdu.edu.cn/showproblem.php?pid=1312 /*HDU 1312 ----- Red and Black 入门搜索 */ #include <cstdio> int n, m; //n行m列 int cnt, startx, starty; char mapp[25][25]; /*找一个连通的区域里可以走的块*/ void dfs(int x, int y){ if (x

HDU 5343 MZL&#39;s Circle Zhou

MZL's Circle Zhou Time Limit: 1000ms Memory Limit: 131072KB This problem will be judged on HDU. Original ID: 534364-bit integer IO format: %I64d      Java class name: Main MZL's Circle Zhou is good at solving some counting problems. One day, he comes

hdu 5343 MZL&#39;s Circle Zhou SAM

MZL's Circle Zhou 题意:给定两个长度不超过a,b(1 <= |a|,|b| <= 90000),x为a的连续子串,b为y的连续子串(x和y均可以是空串):问x+y形成的不同串的个数? 误区:开始一门心思想着求出总的可形成的字符串个数,再减去a,b中相同的子串重叠的部分:想通过连续插入a+b得到的SAM并不能获得信息:因为x,y是任意的子串,连续插入导致一定是a的后缀和b的前缀 正解:直接在计算有不同子串时,就不去计算重复的 <=>对于一个可能出现x后缀和y前缀相同

【后缀自动机】HDU 5343 MZL&#39;s Circle Zhou

通道 题意:从A,B分别取出子串X,Y,求多少种不同的X+Y 思路: 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAX_N = 200007; typedef unsigned long long ll; struct SAM { int val[MAX_N], fa[MAX_N], c[26][MAX_N]; int tot,

hdu 1399 Starship Hakodate-maru (暴力搜索)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1399 题目大意:找到满足i*i*i+j*(j+1)*(j+2)/6形式且小于等于n的最大值. 1 #include<iostream> 2 #include<cstdio> 3 4 using namespace std; 5 6 int main() 7 { 8 int n; 9 while(scanf("%d",&n),n) 10 { 11 int j,

HDU 1885 Key Task 状态压缩+搜索

点击打开链接 Key Task Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1176    Accepted Submission(s): 462 Problem Description The Czech Technical University is rather old - you already know that it c