竞赛讨论区 > D题数据是否有问题?
头像
AE酱
发布于 2020-05-10 17:54
+ 关注

D题数据是否有问题?

D题数据是否有问题?

6 10 2
XRDX..
....D.
.XXX..
......
..XX..
XD....
X..XXX
X....X
......
X.....

答案应该为Yes,而下面这两份AC代码均输出no

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <list>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <stack>
#pragma GCC optimize(3 , "Ofast" , "inline")
using namespace std ;
typedef long long ll ;
const double esp = 1e-6 , pi = acos(-1) ;
typedef pair<int , int> PII ;
const int N = 1e6 + 10 , INF = 0x3f3f3f3f , mod = 1e9 + 7;
int in()
{
  int x = 0 , f = 1 ;
  char ch = getchar() ;
  while(!isdigit(ch)) {if(ch == '-') f = -1 ; ch = getchar() ;}
  while(isdigit(ch)) x = x * 10 + ch - 48 , ch = getchar() ;
  return x * f ;
}
string s[12] ;
int w , h ,flag , d[4][2] = {1 , 0 , -1 , 0 , 0 , 1 , 0 , -1} , k , cnt ;
PII a[10] ;
map<PII , int> mp ;
bool check(int x , int y)
{
  if(x < 0 || y < 0 || y >= w || x >= h || s[x][y] == 'X' || s[x][y] == 'R') return false ;
  return true ;
}
int ans = 0 ;
void dfs(int u)
{
  if(u == k)
   {
     for(int i = 0 ;i < cnt ;i ++ )
       if(mp[a[i]])
        flag = 1;
     // cout << u << endl ;
     // for(int i = 0;i < h ;i ++)
     //  cout << s[i] << endl ;
     // puts("") ;
     return ;
   }
   for(int i = 0 ;i < cnt ;i ++ )
    {
      int x = a[i].first , y = a[i].second ;
      for(int j = 0 ;j < 4 ;j ++ )
       {
         int tx = x , ty = y ;
         while(check(tx + d[j][0] , ty + d[j][1]))
          tx += d[j][0] , ty += d[j][1] ;
         if(tx == x && ty == y) continue ;
         s[x][y] = '.' ;
         char c = s[tx][ty] ;
         s[tx][ty] = 'R' ;
         a[i].first = tx , a[i].second = ty ;
         dfs(u + 1) ;
         s[x][y] = 'R' ;
         s[tx][ty] = c ;
         a[i].first = x , a[i].second = y ;
       }
    }
}
int main()
{
  cin >> w >> h >> k ;
  for(int i = 0 ;i < h ;i ++ ) cin >> s[i] ;
  for(int i = 0 ;i < h ;i ++ )
   for(int j = 0 ;j < w ;j ++ )
    if(s[i][j] == 'R') a[cnt ++] = {i , j} ;
    else if(s[i][j] == 'D') mp[{i , j}] = 1 ;
  dfs(0) ;
  if(flag) puts("YES") ;
  else puts("NO") ;
  return 0 ;
}
#include <stdio.h>
int mov[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
char s[10][20][20];
int w, h, k, res = 0;
void dfs(int time)
{
    if(time == k+1)
    {
        for(int i = 1; i <= h; i++)
            for(int j = 1; j <= w; j++)
            if(s[k][i][j]=='R' && s[0][i][j]=='D') res = 1;
        return ;
    }
    int nowx, nowy, tmpx, tmpy;
    for(int i = 1; i <= h; i++)
        for(int j = 1; j <= w; j++)
        s[time][i][j] = s[time-1][i][j];
    for(int i = 1; i <= h; i++)
        for(int j = 1; j <= w; j++)
        {
            if(s[time-1][i][j]=='R')
            {
                for(int k = 0; k < 4; k++)
                {
                    nowx = i; nowy = j;
                    while(1)
                    {
                        tmpx = nowx + mov[k][0];
                        tmpy = nowy + mov[k][1];
                        if(tmpx < 1 || tmpx > h || tmpy < 1 || tmpy > w) break;
                        if(s[time-1][tmpx][tmpy]=='R'||s[time-1][tmpx][tmpy]=='X') break;
                        nowx = tmpx; nowy = tmpy;
                    }
                    s[time][nowx][nowy] = 'R';
                    s[time][i][j] = '.';
                    dfs(time+1);
                    s[time][nowx][nowy] = '.';
                    s[time][i][j] = 'R';
                }
            }
        }
}
int main()
{

    scanf("%d %d %d", &w, &h, &k);
    for(int i = 1; i <= h; i++)
    scanf("%s", s[0][i]+1);

    dfs(1);

    if(res ) puts("YES");
    else puts("NO");
}

全部评论

(3) 回帖
加载中...
话题 回帖