New Year and Buggy Bot

Posted dwvictor

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

以上是关于New Year and Buggy Bot的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Good Bye 2017 B. New Year and Buggy Bot 枚举全排列模拟

New Year and Permutation

Codeforces 750 E New Year and Old Subsequence

Codeforces 1106CLunar New Year and Number Division

CodeForces - 750D New Year and Fireworks

B - New Year and Ascent Sequence