pku 3083 Children of the Candy Corn

http://poj.org/problem?id=3083

题意就读了很长时间还是没读懂。。。无语。。。懂了之后才开始自己写了一个果断tle郁闷死我了。。最后看了别人很巧妙的方法。。0s过。。。牛啊。不过就是不明白,为什么初始方向没标记也能过。默认的也能过。。。

默认初始方向为0的

View Code
#include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
#define maxn 50
using namespace std;
int vt[maxn][maxn];
char str[maxn][maxn];
int n,m;
struct node
{
int x,y;
int len;
}s;
int dir[4][2]={{1, 0}, {0, -1}, {-1, 0}, {0, 1}};
int d,h;
bool dfs(int x,int y,int len)
{
if (str[x][y] == 'E')
{
printf("%d ",len + 1);
return true;
}
if (x < 0 || x >= n || y < 0 || y >= m || str[x][y] == '#') return false;
d = (d - h + 4)%4;//对于左边时,是向左转,右边时是向右转。
while (1)
{
if (dfs(x + dir[d][0],y + dir[d][1],len + 1))
return true;
d = (d + h)%4;//对于左边时是顺时针寻找,对于右边时是逆时针寻找
}
}
int bfs()
{
queue<node>q;
while (!q.empty()) q.pop();
q.push(s);
while (!q.empty())
{
node tmp = q.front();
q.pop();
if (str[tmp.x][tmp.y] == 'E')
{
return tmp.len + 1;
}
for (int i = 0; i < 4; ++i)
{
node tt;
tt.x = tmp.x + dir[i][0];
tt.y = tmp.y + dir[i][1];
if (tt.x >=0 && tt.x < n && tt.y >=0 && tt.y <m && !vt[tt.x][tt.y] && str[tt.x][tt.y] != '#')
{
tt.len = tmp.len + 1;
vt[tt.x][tt.y] = true;
q.push(tt);
}
}
}
return 0;

}
int main()
{
int t,i,j;
scanf("%d",&t);
while (t--)
{
cin>>m>>n;
for (i = 0; i < n; ++i)
{
for (j = 0; j < m; ++j)
{
cin>>str[i][j];
if (str[i][j] == 'S')
{
s.x = i; s.y = j;
s.len = 0;
}
}
}
d = 0; h = 1;dfs(s.x,s.y,0);
d = 0; h = 3;dfs(s.x,s.y,0);
memset(vt,false,sizeof(vt));
printf("%d\n",bfs());
}
return 0;
}


标记初始方向的。

View Code
#include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
#define maxn 50
using namespace std;
int vt[maxn][maxn];
char str[maxn][maxn];
int n,m;
struct node
{
int x,y;
int len;
}s;
int dir[4][2]={{1, 0}, {0, -1}, {-1, 0}, {0, 1}};
int d,h;
bool dfs(int x,int y,int len)
{
if (str[x][y] == 'E')
{
printf("%d ",len + 1);
return true;
}
if (x < 0 || x >= n || y < 0 || y >= m || str[x][y] == '#') return false;
d = (d - h + 4)%4;//对于左边时,是向左转,右边时是向右转。
while (1)
{
if (dfs(x + dir[d][0],y + dir[d][1],len + 1))
return true;
d = (d + h)%4;//对于左边时是顺时针寻找,对于右边时是逆时针寻找
}
}
int bfs()
{
queue<node>q;
while (!q.empty()) q.pop();
q.push(s);
while (!q.empty())
{
node tmp = q.front();
q.pop();
if (str[tmp.x][tmp.y] == 'E')
{
return tmp.len + 1;
}
for (int i = 0; i < 4; ++i)
{
node tt;
tt.x = tmp.x + dir[i][0];
tt.y = tmp.y + dir[i][1];
if (tt.x >=0 && tt.x < n && tt.y >=0 && tt.y <m && !vt[tt.x][tt.y] && str[tt.x][tt.y] != '#')
{
tt.len = tmp.len + 1;
vt[tt.x][tt.y] = true;
q.push(tt);
}
}
}
return 0;

}
int main()
{
int t,i,j;
scanf("%d",&t);
while (t--)
{
cin>>m>>n;
for (i = 0; i < n; ++i)
{
for (j = 0; j < m; ++j)
{
cin>>str[i][j];
if (str[i][j] == 'S')
{
s.x = i; s.y = j;
s.len = 0;
if (i == 0)
d = 0;
if (i == n - 1)
d = 2;
if (j == 0)
d = 3;
if (j == m - 1)
d = 1;
}
}
}
//d记录开始的方向
h = 1;dfs(s.x,s.y,0);
h = 3;dfs(s.x,s.y,0);
memset(vt,false,sizeof(vt));
printf("%d\n",bfs());
}
return 0;
}


这是过了一段时间自己写的代码0s过的。。这次是自己按着自己的理解过的。前几天一直tle于此题,不知道怎么今天重新敲了一遍0s 1Y...

View Code
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define maxn 44
using namespace std;

struct  node
{
    int x,y;
    int len;
}s,e;
int dir[4][2] = {{0,1},{-1,0},{0,-1},{1,0}};
int n,m,tmp;
bool vt[maxn][maxn];
char str[maxn][maxn];

int bfs()
{
    int sum = 0,i;
    queue<node>q;
    while (!q.empty()) q.pop();
    q.push(s);
    vt[s.x][s.y] = true;
    while (!q.empty())
    {
        node cur = q.front();
        q.pop();
        if (cur.x == e.x && cur.y == e.y)
        {
            sum = cur.len;
            break;
        }
        for (i = 0; i < 4; ++i)
        {
            int tx = cur.x + dir[i][0];
            int ty = cur.y + dir[i][1];
            if (tx >= 0 && tx < n && ty >= 0 && ty < m && !vt[tx][ty] && str[tx][ty] != '#')
            {
                vt[tx][ty] = true;
                node tt;
                tt.x = tx; tt.y = ty;
                tt.len = cur.len + 1;
                q.push(tt);
            }
        }
    }
    return sum;
}
int dfsl(int sx,int sy,int d)
{
    int i,j;
    if (sx == e.x && sy == e.y)
    {
        return 1;
    }
    //先往左走,若果左边不能走再按顺时针检查,能走的!
    for (i = 0,j = d + 1; i < 4; ++i,--j)
    {
        int pos = (j + 4)%4;
        int tx = sx + dir[pos][0];
        int ty = sy + dir[pos][1];
        if (tx >= 0 && tx < n && ty >= 0 && ty < m && str[tx][ty] != '#')
        {
            vt[tx][ty] = true;
            return 1 + dfsl(tx,ty,pos);
            vt[tx][ty] = false;
        }
    }
    return 0;
}
int dfsr(int sx,int sy,int d)
{
    int i,j;
    if (sx == e.x && sy == e.y)
    {
        return 1;
    }
    //先往右走,若果右边不能走再按逆时针检查,能走的!
    for (i = 0,j = d - 1; i < 4; ++i,++j)
    {
        int pos = (j + 4)%4;
        int tx = sx + dir[pos][0];
        int ty = sy + dir[pos][1];
        if (tx >= 0 && tx < n && ty >= 0 && ty < m && str[tx][ty] != '#')
        {
            vt[tx][ty] = true;
            return 1 + dfsr(tx,ty,pos);
            vt[tx][ty] = false;
        }
    }
    return 0;
}
int main()
{
    int t,i,j;
    scanf("%d",&t);
    while (t--)
    {
        scanf("%d%d",&m,&n);
        for (i = 0; i < n; ++i)
        {
            scanf("%s",str[i]);
            for (j = 0; j < m; ++j)
            {
                if (str[i][j] == 'S')
                {
                    s.x = i; s.y = j;
                    s.len = 1;
                    //这里记录初始方向,
                    if (j == 0) tmp = 0;
                    else if (j == m - 1) tmp = 2;
                    else if (i == 0) tmp = 3;
                    else if (i == n - 1) tmp = 1;
                }
                else if (str[i][j] == 'E')
                {
                    e.x = i; e.y = j;
                }
            }
        }
        memset(vt,false,sizeof(vt));
        int l3 = bfs();//bfs是最简单的求最短距离
        //printf("%d\n",l3);
        memset(vt,false,sizeof(vt));
        vt[s.x][s.y] = true;
        int l1 = dfsl(s.x,s.y,tmp);
        //printf(">>%d\n",l1);
        int l2 = dfsr(s.x,s.y,s.len);
        printf("%d %d %d\n",l1,l2,l3);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值