TOJ 2017: N-Credible Mazes

2017: N-Credible Mazes 分享至QQ空间

Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByte
Total Submit: 6            Accepted:0

Description

An n-tersection is defined as a location in n-dimensional space, n being a positive integer, having all non-negative integer coordinates. For example, the location (1,2,3) represents an n-tersection in three dimensional space. Two n-tersections are said to be adjacent if they have the same number of dimensions and their coordinates differ by exactly 1 in a single dimension only. For example, (1,2,3) is adjacent to (0,2,3) and (2,2,3) and (1,2,4), but not to (2,3,3) or (3,2,3) or (1,2). An n-teresting space is defined as a collection of paths between adjacent n-tersections.

Finally, an n-credible maze is defined as an n-teresting space combined with two specific n-tersections in that space, one of which is identified as the starting n-tersection and the other as the ending n-tersection.

Input

The input file will consist of the descriptions of one or more n-credible mazes. The first line of the description will specify n, the dimension of the n-teresting space. (For this problem, n will not exceed 10, and all coordinate values will be less than 10.) The next line will contain 2n non-negative integers, the first n of which describe the starting n-tersection, least dimension first, and the next n of which describe the ending n-tersection. Next will be a nonnegative number of lines containing 2n non-negative integers each, identifying paths between adjacent n-tersections in the n-teresting space. The list is terminated by a line containing only the value ?C1. Several such maze descriptions may be present in the file. The end of the input is signalled by space dimension of zero. No further data will follow this terminating zero.

Output

For each maze output it's position in the input; e.g. the first maze is "Maze #1", the second is "Maze #2", etc. If it is possible to travel through the n-credible maze's n-teresting space from the starting n-tersection to the ending n-tersection, also output "can be travelled" on the same line. If such travel is not possible, output "cannot be travelled" instead.

Sample Input

 

2
0 0 2 2
0 0 0 1
0 1 0 2
0 2 1 2
1 2 2 2
-1
3
1 1 1 1 2 3
1 1 2 1 1 3
1 1 3 1 2 3
1 1 1 1 1 0
1 1 0 1 0 0
1 0 0 0 0 0
-1
0

Sample Output

Maze #1 can be travelled
Maze #2 cannot be travelled
数据的锅,理解题意写的代码大概都是可以AC的

爆搜的代码

#include<stdio.h>
#include<algorithm>
using namespace std;
int a[1005][10],b[1005][10],c[1005][3];
int main()
{
    int n,T=1;
    while(~scanf("%d",&n),n)
    {
        int i,j,k;
        for(i=0;;i++)
        {
            scanf("%d",&a[i][0]);
            if(a[i][0]==-1)break;
            for(j=1; j<n; j++)
                scanf("%d",&a[i][j]);
            for(j=0; j<n; j++)
                scanf("%d",&b[i][j]);
        }
        for(k=0; k<i; k++)
            for(j=0; j<n; j++)
                c[k][j]=0;
        c[0][0]=1;
        for(i=0; i<k; i++)
        {
            int f=0;
            for(j=0; j<n; j++)
                if(a[i+1][j]!=a[i][j])
                {
                    f++;
                    if(abs(a[i+1][j]-a[i][j])!=1)break;
                }
            if(f<=1&&j==n&&c[i][0]==1)c[i+1][0]=1;
            f=0;
            for(j=0; j<n; j++)
                if(b[i][j]!=a[i][j])
                {
                    f++;
                    if(abs(b[i][j]-a[i][j])!=1)break;
                }
            if(f<=1&&j==n&&c[i][0]==1)c[i][1]=1;
            f=0;
            for(j=0; j<n; j++)
                if(b[i+1][j]!=b[i][j])
                {
                    f++;
                    if(abs(b[i+1][j]-b[i][j])!=1)break;
                }
            if(f<=1&&j==n&&c[i][1]==1)c[i+1][1]=1;
            f=0;
            for(j=0; j<n; j++)
                if(b[i+1][j]!=a[i+1][j])
                {
                    f++;
                    if(abs(b[i+1][j]-a[i+1][j])!=1)break;
                }
            if(f<=1&&j==n&&c[i+1][1]==1)c[i+1][0]=1;
        }
        if(c[k-1][1]>=1) printf("Maze #%d can be travelled\n",T++);
        else printf("Maze #%d cannot be travelled\n",T++);
    }
    return 0;
}

map+set维护

#include<stdio.h>
#include<vector>
#include<set>
#include<map>
using namespace std;
map<int,vector<int> >M;
set<int>S;
int la(int &num,int n)
{
    num=0;
    for(int i=1; i<=n; i++)
    {
        int x;
        scanf("%d",&x);
        if(x==-1)return 0;
        num=num*10+x;
    }
    return 1;
}
int dfs(int st,int ed)
{
    if(st==ed)return 1;
    int l=M[st].size();
    for(int i=0; i<l; i++)
    {
        int v=M[st][i];
        if(!S.count(v))
        {
            S.insert(v);
            if(dfs(v,ed))return 1;
        }
    }
    return 0;
}
int main()
{
    int t=1,n;
    while(~scanf("%d",&n))
    {
        if(n==0)
        {
            if(scanf("%d",&n)==EOF)
                break;
        }
        M.clear(),S.clear();
        int st,ed;
        la(st,n);
        la(ed,n);
        int s,e;
        while(la(s,n))
        {
            la(e,n);
            M[s].push_back(e);
            M[e].push_back(s);
        }
        if(dfs(st,ed))
            printf("Maze #%d can be travelled\n",t++);
        else
            printf("Maze #%d cannot be travelled\n",t++);
    }
    return 0;
}

网上的HDUac代码

#include<stdio.h>
#include<string.h>
int f,sum,e,s;
int a[10010],b[10010];
int mark[10010];
void dfs(int w)
{
    int i;
    if (w==e)
    {
        f=1;
        return ;
    }
    for (i=1; i<=sum; i++)
        if (mark[i]==0 && (a[i]==w || b[i]==w))
        {
            mark[i]=1;
            if (a[i]==w) dfs(b[i]);
            else dfs(a[i]);
        }
}


int main()
{
    int Case,i,j,n,x;
    Case=0;
    while (scanf("%d",&n)!=EOF)
    {
        if (n==0) break;

        Case++;
        s=e=0;
        for (i=1; i<=n; i++)
        {
            scanf("%d",&x);
            s=s*10+x;
        }
        for (i=1; i<=n; i++)
        {
            scanf("%d",&x);
            e=e*10+x;
        }

        sum=0;
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));

        while (scanf("%d",&x)!=EOF)
        {
            if (x==-1) break;

            sum++;
            a[sum]=x;
            for (i=1; i<n; i++)
            {
                scanf("%d",&x);
                a[sum]=a[sum]*10+x;
            }
            for (i=1; i<=n; i++)
            {
                scanf("%d",&x);
                b[sum]=b[sum]*10+x;
            }

        }
        f=0;
        memset(mark,0,sizeof(mark));
        dfs(s);
        printf("Maze #%d ",Case);
        if (f==0)
            printf("cannot be travelled\n");
        else
            printf("can be travelled\n");
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/BobHuang/p/7697718.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值