[BZOJ1501/NOI2005]智慧珠游戏

Input

文件中包含初始的盘件描述,一共有10行,第i行有i个字符。如果第i行的第j个字符是字母”A”至”L”中的一个,则表示第i行第j列的格子上已经放了零件,零件的编号为对应的字母。如果第i行的第j个字符是”.”,则表示第i行第j列的格子上没有放零件。输入保证预放的零件已摆放在盘件中。

Output

如果能找到解,向输出文件打印10行,为放完全部12个零件后的布局。其中,第i行应包含i个字符,第i行的第j个字符表示第i行第j列的格子上放的是哪个零件。如果无解,输出单独的一个字符串‘No solution’(不要引号,请注意大小写)。所有的数据保证最多只有一组解。

Sample Input

.
..
...
....
.....
.....C
...CCC.
EEEHH...
E.HHH....
E.........

Sample Output

B
BK
BKK
BJKK
JJJDD
GJGDDC
GGGCCCI
EEEHHIIA
ELHHHIAAF
ELLLLIFFFF
 
备注:题面建议大家去BZOJ官网去看,因为格式问题这里的图片只能这么小。BZOJ 1051:http://www.lydsy.com/JudgeOnline/problem.php?id=1501
 
 
 
题解:
  实在没什么好讲的。。。虽然说这次校内的考试中我把80分的写出来了(有10分是忘了No solution。。无语),但是无法保证在考场上能够有如此良好的心态写个2小时!我们取每个图案的每个方向,记以左上角的那个点为原点,记录其他点的相对关系。然后在地图上一个个位置进行判断是否可行。思路并不复杂,但是记录其他点得相对关系这个部分,写得丑的可以写出几10k+,写得好的也要6k。
  但是如果就是这么做的话,只有90分。因为有一个数据点故意卡!我觉得这个数据真的不明觉厉,有种卡得莫名其妙,因为他唯一已经放置的智慧珠在右下方,有且仅有一个,所有前面部分搜的时间特别长,所以要把地图调换一下方向。这谁会这么去想啊!而且即便就是这么做了,其实一样地,如果我把唯一的一个智慧珠放在左下角,是没有任何办法通过的,所以这道题还是存在很多疑问的。
  还有一种做法是Dacing-Link,我没有去了解,可以上网查查,你会发现就查到这一道题了,所以并没有什么意义。
 
代码(90分):
------------------------------------------------------------------------------------------------------

#include <cstdio>
#include <cstdlib>
#define MAXN 15
#define DFS_next if (y == x) DFS(x + 1, 1); else DFS(x, y + 1)

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0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0};

int vis[MAXN][MAXN], map[MAXN][MAXN], use[MAXN];

bool out(int o) { return o < 1 || o > 10; }

bool check(int o, int x, int y)
{
  if (inv[o]) return 0;
  for (int i = 1; i < tot[o]; i++)
  {
    int dx = x + a[o][i], dy = y + b[o][i];
    if (out(dx) || out(dy)) return 0;
    if (vis[dx][dy]) return 0;
  }
  return 1;
}

void DFS(int x, int y)
{
  if (x == 11 && y == 1)
  {
    for (int i = 1; i <= 10; i++)
    {
      for (int j = 1; j <= i; j++) printf("%c",map[i][j] + 'A' - 1);
      printf("\n");
    }
    exit(0);
  }
  if (vis[x][y]) { DFS_next; }
  else for (int i = 1; i <= 12; i++)
  {
    if (use[i]) continue;
    for (int j = 0; j <= 7; j++)
    {
      int o = i * 8 - j;
      if (check(o, x, y))
      {
        for (int k = 0; k < tot[o]; k++)
          vis[x + a[o][k]][y + b[o][k]] = 1, map[x + a[o][k]][y + b[o][k]] = i;
        use[i] = 1; DFS_next; use[i] = 0;
        for (int k = 0; k < tot[o]; k++) vis[x + a[o][k]][y + b[o][k]] = 0;
      }
    }
  }
}

char ch[MAXN];

int main()
{
  freopen("game.in", "r", stdin);
  freopen("game.out", "w", stdout);
  for (int i = 1; i <= 10; i++)
  {
    scanf("%s",ch);
    for (int j = 0; j <= i - 1; j++)
    {
      if (ch[j] == '.') map[i][j + 1] = 0;
      else map[i][j + 1] = ch[j] - 'A' + 1, vis[i][j + 1] = 1, use[ch[j] - 'A' + 1] = 1;
    }
  }
  for (int i = 1; i <= 10; i++)
    for (int j = 10; j > i; j--) vis[i][j] = 1;
  DFS(1,1);
  printf("No solution");
  return 0;
}

------------------------------------------------------------------------------------------------------

转载于:https://www.cnblogs.com/jinkun113/p/4876150.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值