• New Year and Buggy Bot


    全排列

    https://www.cnblogs.com/DWVictor/p/10301666.html

    #include <bits/stdc++.h>
    using namespace std;
    int dir[4][2]={1,0,-1,0,0,1,0,-1};
    int Hash[4];
    char mmap[55][55];
    struct xjy
    {
        int x;
        int y;
    };
    int main()
    {
        for(int i=0;i<4;i++)
        {
            Hash[i]=i;
        }
        for(int i=0;i<55;i++)
            for(int j=0;j<55;j++)
        {
            mmap[i][j]='#';
        }
        int n,m;
        cin >> n >>m;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                scanf(" %c",&mmap[i][j]);
        string re;
        cin >> re;
        xjy start,eend;
        for(int i=1;i<=n;i++)
           {
                for(int j=1;j<=m;j++)
                {
                    //cout << mmap[i][j] ;
                    if(mmap[i][j]=='S')
                    {
                        start.x=i;
                        start.y=j;
                    }
                    else if(mmap[i][j]=='E')
                    {
                        eend.x=i;
                        eend.y=j;
                    }
                }
                //cout <<endl;
           }
     
        int cnt=0;
        do
        {
            xjy mid;
            mid.x=start.x;
            mid.y=start.y;
            //cout << mid.x << " " << mid.y << endl;
            for(int i=0;i<re.length();i++)
            {
                mid.x=mid.x+dir[Hash[re[i]-'0']][0];
                mid.y=mid.y+dir[Hash[re[i]-'0']][1];
                //cout << mid.x << " " << mid.y << endl;
                if(mmap[mid.x][mid.y]=='E')
                {
                    cnt++;
                    break;
                }
                else if(mmap[mid.x][mid.y]=='#')
                {
                    break;
                }
            }
        }while(next_permutation(Hash,Hash+4));
        cout << cnt <<endl;
    }
    View Code

    暴力枚举

    #include<stdio.h>
    #include<string.h>
     
    char mp[55][55],s[110];
    int a[4],book[4],x1,x2,y1,y2,sum,n,m,l;
    int nx[]={0,1,0,-1};
    int ny[]={1,0,-1,0};
     
    void dfs(int cnt)
    {
        if(cnt==4)
          {
              int tx=x1,ty=y1;
              for(int i=0;i<l;i++) 
            {
                tx+=nx[a[s[i]-'0']];
                ty+=ny[a[s[i]-'0']];
                if(tx<1||tx>n||ty<1||ty>m||mp[tx][ty]=='#') return;
                if(tx==x2&&ty==y2) 
                  {
                      sum++;
                    return;
                  }
            }
          }
        else
        {
            for(int i=0;i<4;i++)
            {
                if(book[i]==0)
                  {
                      a[cnt]=i;
                    book[i]=1;
                      dfs(cnt+1);
                      book[i]=0;
                  }
            }
        }
            
    }
     
    int main()
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++) 
          {
              scanf("%s",mp[i]+1);
              for(int j=1;j<=m;j++) 
            {
                if(mp[i][j]=='S') {x1=i;y1=j;}
                else if(mp[i][j]=='E') {x2=i;y2=j;}
            }  
          }
        scanf("%s",s);
        l=strlen(s);
        sum=0;
        dfs(0);
        printf("%d",sum);
        return 0;
    }
    View Code
  • 相关阅读:
    服务返返回状态码详解
    LeetCode#28 Implement strStr()
    LeetCode#58 Length of Last Word
    LeetCode#66 Plus One
    spooling技术
    文件的打开与关闭
    DMA方式与通道方式
    中断向量、向量中断、向量地址
    中断响应优先级和中断处理优先级
    I/O接口
  • 原文地址:https://www.cnblogs.com/DWVictor/p/10301671.html
Copyright © 2020-2023  润新知