HDU4740-模拟

题目:题目链接

 

题意:就是驴子和老虎在一个森林中跑,要是他们相遇了就输出相遇坐标,否则输出-1.跑动的原则是不跑出森林并且不跑自己跑过的路

 

分析:刚刚开始用一直搜索找出驴子和老虎各自走的路径,结果华丽丽的爆栈了,如下:

 

#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <map>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <queue>
#include <set>
#include <stack>
#include <functional>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cassert>
#include <bitset>
#include <stack>
#include <ctime>
#include <list>
#define INF 0x7fffffff
#define max3(a,b,c) (max(a,b)>c?max(a,b):c)
#define min3(a,b,c) (min(a,b)<c?min(a,b):c)
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
#define maxn 1001
int QuickMod(int  a,int b,int n)
{
    int r = 1;
    while(b)
    {
        if(b&1)
            r = (r*a)%n;
        a = (a*a)%n;
        b >>= 1;
    }
    return r;
}
int vis1[maxn][maxn];
int vis2[maxn][maxn];
int n;
int st1x, st1y, dir1;
int st2x, st2y, dir2;
int fp1;
int fp2;
int dirx[4] = {0, 1, 0, -1};
int diry[4] = {1, 0, -1, 0};
bool inmap(int x, int y)
{
    if(x < 0 || x >= n || y < 0 || y >= n)
        return false;
    return true;
}
int xx1, yy1;
int flag;
int xx2, yy2;
struct node
{
    int x, y;
} num[maxn], cop[maxn];
int cntx;
int cnty;
void DFSX(int x1, int y1, int d1)
{
    if(fp1)
        return;
    vis1[x1][y1] = 1;
    int xp = x1 + dirx[d1];
    int yp = y1 + diry[d1];
    if(inmap(xp, yp) && !vis1[xp][yp])
    {
        xx1 = xp;
        yy1 = yp;
        cntx ++;
        num[cntx].x = xx1;
        num[cntx].y = yy1;
        DFSX(xx1, yy1, d1);
    }
    else
    {
        if(d1 == 3)
        {
            int tx = x1 + dirx[0];
            int ty = y1 + diry[0];
            if(!vis1[tx][ty] && inmap(tx, ty))
            {
                xx1 = tx;
                yy1 = ty;
                cntx ++;
                num[cntx].x = xx1;
                num[cntx].y = yy1;
                DFSX(xx1, yy1, 0);
            }
            else
                fp1 = 1;
        }
        else
        {
            int tx = x1 + dirx[d1+1];
            int ty = y1 + diry[d1+1];
            if(!vis1[tx][ty] && inmap(tx, ty))
            {
                xx1 = tx;
                yy1 = ty;
                cntx ++;
                num[cntx].x = xx1;
                num[cntx].y = yy1;
                DFSX(xx1, yy1, d1+1);
            }
            else
                fp1 = 1;
        }
    }
    if(fp1)
        return;
}
void DFSY(int x1, int y1, int d1)
{
    if(fp2)
        return;
    vis2[x1][y1] = 1;
    int xp = x1 + dirx[d1];
    int yp = y1 + diry[d1];
    if(inmap(xp, yp) && !vis2[xp][yp])
    {
        xx2 = xp;
        yy2 = yp;
        cnty++;
        cop[cnty].x = xx2;
        cop[cnty].y = yy2;
        DFSY(xx2, yy2, d1);
    }
    else
    {
        if(d1 == 0)
        {
            int tx = x1 + dirx[3];
            int ty = y1 + diry[3];
            if(!vis2[tx][ty] && inmap(tx, ty))
            {
                xx2 = tx;
                yy2 = ty;
                cnty++;
                cop[cnty].x = xx2;
                cop[cnty].y = yy2;
                DFSY(xx2, yy2, 3);
            }
            else
                fp2 = 1;
        }
        else
        {
            int tx = x1 + dirx[d1-1];
            int ty = y1 + diry[d1-1];
            if(!vis2[tx][ty] && inmap(tx, ty))
            {
                xx2 = tx;
                yy2 = ty;
                cnty++;
                cop[cnty].x = xx2;
                cop[cnty].y = yy2;
                DFSY(xx2, yy2, d1-1);
            }
            else
                fp2 = 1;
        }
    }
    if(fp2)
        return;
}

int main()
{
    while(scanf("%d", &n))
    {
        if(!n)
            break;
        mem(vis1, 0);
        mem(vis2, 0);
        mem(num, 0);
        cntx = 0;
        cnty = 0;
        scanf("%d%d%d", &st1x, &st1y, &dir1);
        scanf("%d%d%d", &st2x, &st2y, &dir2);
        fp1 = 0;
        fp2 = 0;
        xx1 = st1x;
        yy1 = st1y;
        xx2 = st2x;
        yy2 = st2y;
        flag = 0;
        DFSX(st1x, st1y, dir1);
        DFSY(st2x, st2y, dir2);
        int inde = min(cntx, cnty);
        int i;
        for(i = 1; i < inde; ++i)
        {
            if(num[i].x == cop[i].x && num[i].y == cop[i].y)
            {
                flag = 1;
                break;
            }
        }
        if(flag)
            printf("%d %d\n", num[i].x, num[i].y);
        else
            printf("-1\n");
    }
    return 0;
}


 

 

后面就使用一步步判断的方法搞,及时跳出,这样就可以了。我又犯昨天的错误了,没有判断起点相同,WA了好几次尴尬:如下:

 

#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <map>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <queue>
#include <set>
#include <stack>
#include <functional>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cassert>
#include <bitset>
#include <stack>
#include <ctime>
#include <list>
#define INF 0x7fffffff
#define max3(a,b,c) (max(a,b)>c?max(a,b):c)
#define min3(a,b,c) (min(a,b)<c?min(a,b):c)
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
#define maxn 1001
int vis1[maxn][maxn];
int vis2[maxn][maxn];
int n;
int st1x, st1y, dir1;
int st2x, st2y, dir2;
int fp1;
int fp2;
int dirx[4] = {0, 1, 0, -1};
int diry[4] = {1, 0, -1, 0};
bool inmap(int x, int y)
{
    if(x < 0 || x >= n || y < 0 || y >= n)
        return false;
    return true;
}

int xx1, yy1;
int flag;
int xx2, yy2;
void work(int x1, int y1, int d1, int x2, int y2, int d2)
{
    while(!fp1 || !fp2)
    {
        if(!fp1)
        {
            vis1[x1][y1] = 1;
            int tx = x1 + dirx[d1];
            int ty = y1 + diry[d1];
            if(!vis1[tx][ty] && inmap(tx, ty))
            {
                x1 = tx;
                y1 = ty;
            }
            else
            {
                if(d1 == 3)
                {
                    tx = x1 + dirx[0];
                    ty = y1 + diry[0];
                    if(inmap(tx, ty) && !vis1[tx][ty])
                    {
                        x1 = tx;
                        y1 = ty;
                        d1 = 0;
                    }
                    else
                        fp1 = 1;
                }
                else
                {
                    tx = x1 + dirx[d1+1];
                    ty = y1 + diry[d1+1];
                    if(inmap(tx, ty) && !vis1[tx][ty])
                    {
                        x1 = tx;
                        y1 = ty;
                        d1 += 1;
                    }
                    else
                        fp1 = 1;
                }
            }
        }
        if(!fp2)
        {
            vis2[x2][y2] = 1;
            int tx = x2 + dirx[d2];
            int ty = y2 + diry[d2];
            if(!vis2[tx][ty] && inmap(tx, ty))
            {
                x2 = tx;
                y2 = ty;
            }
            else
            {
                if(d2 == 0)
                {
                    tx = x2 + dirx[3];
                    ty = y2 + diry[3];
                    if(inmap(tx, ty) && !vis2[tx][ty])
                    {
                        x2 = tx;
                        y2 = ty;
                        d2 = 3;
                    }
                    else
                        fp2 = 1;
                }
                else
                {
                    tx = x2 + dirx[d2-1];
                    ty = y2 + diry[d2-1];
                    if(inmap(tx, ty) && !vis2[tx][ty])
                    {
                        x2 = tx;
                        y2 = ty;
                        d2 -= 1;
                    }
                    else
                        fp2 = 1;
                }
            }
        }
        if(x1 == x2 && y1 == y2)
        {
            flag = 1;
            printf("%d %d\n", x1, y1);
            break;
        }
    }
}

int main()
{
    while(scanf("%d", &n))
    {
        if(!n)
            break;
        mem(vis1, 0);
        mem(vis2, 0);
        scanf("%d%d%d", &st1x, &st1y, &dir1);
        scanf("%d%d%d", &st2x, &st2y, &dir2);
        fp1 = 0;
        fp2 = 0;
        xx1 = st1x;
        yy1 = st1y;
        xx2 = st2x;
        yy2 = st2y;
        flag = 0;
        if(st1x == st2x && st1y == st2y)
        {
            printf("%d %d\n", st1x, st1y);
            continue;
        }
        else
        {
            work(st1x, st1y, dir1, st2x, st2y, dir2);
            if(!flag)
                printf("-1\n");
        }
    }
    return 0;
}


唉,做了好久。。。。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值