EVERYTHING HAPPENS FOR THE BEST!|

wnsyou

园龄:2年4个月粉丝:19关注:16

2023-05-17 20:32阅读: 27评论: 0推荐: 0

P6201 & P1985 Fliptile S 题解

Fliptile S & 翻转棋 Fliptile S

暴力-47pts

搜索每个格子翻或不翻,最后判断是否每个点都为零即可

点击查看代码
#include <bits/stdc++.h>
using namespace std;
const int dx[5] = {-1, 0, 0, 1, 0}, dy[5] = {0, -1, 1, 0, 0};
int n, m, a[20][20], ans[400], b[20][20], c[400], ansum;
bool f;
bool check(){
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++){
if (b[i][j]){
return 0;
}
}
}
return 1;
}
void dfs(int x, int y, int t, int sum){
if (sum > ansum){
return ;
}
if (f && sum == ansum && c[t - 1] > ans[t - 1]){
return ;
}
if (x == n && y == m + 1){
if (check()){
if (!f || sum < ansum){
for (int i = 1; i < t; i++){
ans[i] = c[i];
}
f = 1;
return ;
}
for (int i = 1; i < t; i++){
if (c[i] < ans[i]){
for (int j = 1; j < t; j++){
ans[i] = c[i];
}
break;
} else if (c[i] > ans[i]){
break;
}
}
}
return ;
}
if (y == m + 1){
dfs(x + 1, 1, t, sum);
return ;
}
for (int i = 0; i < 5; i++){
b[x + dx[i]][y + dy[i]] = !b[x + dx[i]][y + dy[i]];
}
c[t] = 1;
dfs(x, y + 1, t + 1, sum + 1);
for (int i = 0; i < 5; i++){
b[x + dx[i]][y + dy[i]] = !b[x + dx[i]][y + dy[i]];
}
c[t] = 0;
dfs(x, y + 1, t + 1, sum);
}
int main() {
cin >> n >> m;
ansum = n * m + 1;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++){
cin >> a[i][j];
b[i][j] = a[i][j];
}
}
dfs(1, 1, 1, 0);
if (!f){
cout << "IMPOSSIBLE";
} else {
for (int i = 1; i <= n * m; i++){
cout << ans[i] << ' ';
if (i % m == 0){
cout << '\n';
}
}
}
return 0;
}

正解-100pts

首先,在字典序最小的情况下,翻的次数只有两种

  1. 翻奇数次相当于翻 1
  2. 翻偶数次相当于翻 0

当第一排固定之后,后面翻或不翻都是固定的。

  • 当第一排固定后,如果上一行的对应位置为 1,那么当前位置 必须 翻一次,依此类推

最后只需判断第 M 行是否全为零即可。

点击查看代码
#include <bits/stdc++.h>
using namespace std;
const int dx[5] = {-1, 0, 0, 1, 0}, dy[5] = {0, -1, 1, 0, 0};
int n, m, a[20][20], ans[400], b[20][20], leb[20][20], c[400], sum, ansum;
bool f;
void s(int x, int sum){
if (x == m + 1){
int num = 0, t = m + 1;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++){
leb[i][j] = b[i][j];
}
}
// 之后每行都是固定的
for (int i = 2; i <= n; i++){
for (int j = 1; j <= m; j++){
if (b[i - 1][j]){
num++, c[t] = 1;
for (int k = 0; k < 5; k++){
b[i + dx[k]][j + dy[k]] = !b[i + dx[k]][j + dy[k]];
}
} else {
c[t] = 0;
}
t++;
}
}
bool flag = 1;
for (int i = 1; i <= m; i++){
if (b[n][i]){ // 有内鬼!
flag = 0;
break;
}
}
if (flag && sum + num < ansum){ // 没有内鬼并且翻的次数少些
f = 1;
ansum = sum + num;
for (int i = 1; i <= n * m; i++){
ans[i] = c[i];
}
}
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++){
b[i][j] = leb[i][j];
}
}
return ;
}
s(x + 1, sum); // 注意!先选不翻的情况才能使字典序最小
// 翻!
for (int j = 1; j < 5; j++){
b[1 + dx[j]][x + dy[j]] = !b[1 + dx[j]][x + dy[j]];
}
c[x] = 1;
s(x + 1, sum + 1);
// 回溯
c[x] = 0;
for (int j = 1; j < 5; j++){
b[1 + dx[j]][x + dy[j]] = !b[1 + dx[j]][x + dy[j]];
}
}
int main() {
cin >> n >> m;
ansum = n * m + 1;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++){
cin >> a[i][j];
b[i][j] = a[i][j];
}
}
s(1, 0);
if (!f){ // 不成立
cout << "IMPOSSIBLE";
} else {
for (int i = 1; i <= n * m; i++){
cout << ans[i] << ' ';
if (i % m == 0){
cout << '\n';
}
}
}
return 0;
}

本文作者:wnsyou の blog

本文链接:https://www.cnblogs.com/wnsyou-blog/p/17410126.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   wnsyou  阅读(27)  评论(0编辑  收藏  举报
  1. 1 勝利への道 安藤浩和
  2. 2 Minecraft’s End Eric Fullerton
  3. 3 月光曲完整版 贝多芬 云熙音乐
  4. 4 平凡之路 (Live版) 朴树
  5. 5 Minecraft C418
  6. 6 Paradise NiziU
  7. 7 叫我,灰原哀 龙大人不喷火
  8. 8 心机之蛙,一直摸你肚子 ——《名侦探柯南》原创同人曲 炊饭,叶辞樱,温海,寒砧,南柯柯,小茜玛姬,盛姝,阿崔Ac,贝壳初,千湛,兮茶子DaYu,乔慕,黎鹿北,起千温卿,遮阳伞,曲悠
  9. 9 战 歌 此去经年
Minecraft’s End - Eric Fullerton
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

I see the player you mean.

It is reading our thoughts as though they were words on a screen.

They used to hear voices.

Before players could read.

Sometimes disturbing.

Sometimes beautiful indeed.

Does it know that we love it?

That the universe is kind?

A million years ago,it still works

in the reality behind

and the universe said I love you

and the universe said you are the daylight

and the universe said you are not alone

and the universe said you are the night

Once we were called

the spirit of the mountain.

WHO ARE WE

Father sun

Mother moon

Gods demons angels aliens

the player,too.

WE ARE THE UNIVERSE.

We are EVERYTHING you think isn‘t you.

You are alive

ON A FLAT

INFINITE WORLD

generated by a source code

a million years old

and the universe said I love you

and the universe said you are the daylight

and the universe said you are not alone

and the universe said you are the night

Take a breath,now

Take another

Feel air in your lungs.

dreamed it was lost in a story.

and the game was over

Wake up.

加载中…

{{tag.name}}

{{tran.text}}{{tran.sub}}
无对应文字
有可能是
{{input}}
尚未录入,我来提交对应文字
评论
收藏
关注
推荐
深色
回顶
收起
点击右上角即可分享
微信分享提示