Fliptile【搜索】

Fliptile

?POJ - 3279?

Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He has arranged a brainy activity for cows in which they manipulate an?M×?N?grid (1 ≤?M?≤ 15; 1 ≤?N?≤ 15) of square tiles, each of which is colored black on one side and white on the other side.

As one would guess, when a single white tile is flipped, it changes to black; when a single black tile is flipped, it changes to white. The cows are rewarded when they flip the tiles so that each tile has the white side face up. However, the cows have rather large hooves and when they try to flip a certain tile, they also flip all the adjacent tiles (tiles that share a full edge with the flipped tile). Since the flips are tiring, the cows want to minimize the number of flips they have to make.

Help the cows determine the minimum number of flips required, and the locations to flip to achieve that minimum. If there are multiple ways to achieve the task with the minimum amount of flips, return the one with the least lexicographical ordering in the output when considered as a string. If the task is impossible, print one line with the word "IMPOSSIBLE".

Input

Line 1: Two space-separated integers:?M?and?N?
Lines 2..?M+1: Line?i+1 describes the colors (left to right) of row i of the grid with?N?space-separated integers which are 1 for black and 0 for white

Output

Lines 1..?M: Each line contains?N?space-separated integers, each specifying how many times to flip that particular location.

Sample Input

4 4
1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1

Sample Output

0 0 0 0
1 0 0 1
1 0 0 1
0 0 0 0

這題有點坑,一開始完全沒想到搜索,思路是想將第一行所有踩的情況遍歷一遍,后面搜索到第n行,從第二行到第n行每一行的目的都是將其前一行的1轉化為0,最后判斷第n行是否為全為0.

以下是我的代碼:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
int ans;
int num;
int s[20][20];
int mapp[20][20];
int m,n;
int a[20][20];
int b[20][20];
void play(int x)
{ms(a);rep(i,1,n) {rep(j,1,m) {s[i][j]=mapp[i][j];}}num=0;lep(j,m,1) {if(x&1) {s[1][j-1]=!s[1][j-1];s[1][j+1]=!s[1][j+1];s[1][j]=!s[1][j];s[2][j]=!s[2][j];a[1][j]=1;num++;}x>>=1;}rep(i,2,n) {rep(j,1,m) {if(s[i-1][j]==1) {s[i][j-1]=!s[i][j-1];s[i][j+1]=!s[i][j+1];s[i][j]=!s[i][j];s[i+1][j]=!s[i+1][j];s[i-1][j]=!s[i-1][j];a[i][j]=1;num++;}}}rep(i,1,n) {rep(j,1,m) {if(s[i][j]==1)num+=ans;}}
}
void cp()
{rep(i,1,n) {rep(j,1,m) {b[i][j]=a[i][j];}}
}
int main()?
{//freopen("in.txt", "r", stdin);//freopen("out.txt", "w", stdout);ios::sync_with_stdio(0),cin.tie(0);cin>>n>>m;ms(b);rep(i,1,n) {rep(j,1,m) {cin>>mapp[i][j];}}ans=maxn;int x=(1<<m);rep(i,0,x-1) {play(i);if(num<ans) {ans=num;cp();}}if(ans==maxn) {cout<<"IMPOSSIBLE"<<endl;}else {rep(i,1,n) {rep(j,1,m) {cout<<b[i][j]<<" ";}cout<<endl;}?
}return 0;
}

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/536399.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/536399.shtml
英文地址,請注明出處:http://en.pswp.cn/news/536399.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

JS異步開發總結

1 前言 眾所周知&#xff0c;JS語言是單線程的。在實際開發過程中都會面臨一個問題&#xff0c;就是同步操作會阻塞整個頁面乃至整個瀏覽器的運行&#xff0c;只有在同步操作完成之后才能繼續進行其他處理&#xff0c;這種同步等待的用戶體驗極差。所以JS中引入了異步編程&…

迷宮問題【廣搜】

迷宮問題 POJ - 3984 定義一個二維數組&#xff1a; int maze[5][5] {0, 1, 0, 0, 0,0, 1, 0, 1, 0,0, 0, 0, 0, 0,0, 1, 1, 1, 0,0, 0, 0, 1, 0,}; 它表示一個迷宮&#xff0c;其中的1表示墻壁&#xff0c;0表示可以走的路&#xff0c;只能橫著走或豎著走&#xff0c;不能…

大蝦對51單片機入門的經驗總結

回想起當初學習AT89S52的日子還近在眼前:畢業后的第一年呆在親戚公司做了10個月設備管理.乏味的工作和繁雜的瑣事讓我郁悶不已.思考很久后終于辭職.投奔我的同學去了,開始并不曾想到要進入工控行業,知識想找一份電子類技術職業,至于什么職業我根本沒有目標可言.經過兩個多月的挫…

mac安裝cnpm

1.先安裝node node的下載地址&#xff1a;http://nodejs.cn/download/ 這個沒什么好說的&#xff0c;安裝完成后測試一下&#xff0c;在終端輸入&#xff1a;node -v 這時候就可以看到安裝的node版本號&#xff0c;再輸入&#xff1a;npm -v 這時候就會看到npm的版本號了 2.用n…

A計劃【廣搜】

A計劃 HDU - 2102 可憐的公主在一次次被魔王擄走一次次被騎士們救回來之后&#xff0c;而今&#xff0c;不幸的她再一次面臨生命的考驗。魔王已經發出消息說將在T時刻吃掉公主&#xff0c;因為他聽信謠言說吃公主的肉也能長生不老。年邁的國王正是心急如焚&#xff0c;告招天…

WordPress忘記密碼的5種解決方法

為什么80%的碼農都做不了架構師&#xff1f;>>> 無意中忘記wordpress的密碼了&#xff0c;恰巧在后臺又沒來得及設置郵件&#xff0c;只好四處苦尋解決辦法&#xff0c;還好總算找到了…… 1. WordPress內置的找加密碼方法 如果你的admin帳戶的電子郵件地址是正確的…

記錄一次,事務遇到消息發送,疏忽給自己挖坑

場景&#xff1a;一個異步重算功能&#xff08;任務新建后發送消息到RocketMq&#xff09;&#xff0c;每次重算單條記錄的時候&#xff0c;可以計算正確&#xff0c;但是當多條記錄批量重算時&#xff0c;結果總是莫名其妙的不對。排查了很久&#xff0c;終于找到原因 原因&am…

在linux上執行.net Console apps

為什么80%的碼農都做不了架構師&#xff1f;>>> 有個程序&#xff0c;在.net下寫了半天&#xff0c;總算跑起來了&#xff0c;發現有個問題&#xff0c;在windows上不好弄&#xff0c;而同事前一段時間已經有Linux下的解決方法了&#xff0c;于是想直接將.net程序放…

Android4.0設置界面修改總結

為什么80%的碼農都做不了架構師&#xff1f;>>> 筆者前段時間完成設置的圓角item風格的修改&#xff0c;但最近&#xff0c;客戶新增需求&#xff0c;想把設置做成Tab風格的&#xff0c;沒辦法&#xff0c;顧客就是上帝&#xff0c;咱得改啊。今天算是初步改完了&a…

敵兵布陣【線段樹】

敵兵布陣 HDU - 1166 C國的死對頭A國這段時間正在進行軍事演習&#xff0c;所以C國間諜頭子Derek和他手下Tidy又開始忙乎了。A國在海岸線沿直線布置了N個工兵營地,Derek和Tidy的任務就是要監視這些工兵營地的活動情況。由于采取了某種先進的監測手段&#xff0c;所以每個工兵…

Android之仿網易V3.5新特性

為什么80%的碼農都做不了架構師&#xff1f;>>> 最近&#xff0c;網易新聞更新到V3.5了&#xff0c;給我印象最深的是第一次進應用時顯示新特性的ViewPager變成垂直滑動了。于是&#xff0c;小小的模仿了一下&#xff0c;我們來看看效果&#xff1a; 本文源碼下載地…

Android_內存泄露

2019獨角獸企業重金招聘Python工程師標準>>> 1.資源對象沒關閉造成的內存泄漏 描述&#xff1a; 資源性對象比如&#xff08;Cursor&#xff0c;File文件等&#xff09;往往都用了一些緩沖&#xff0c;我們在不使用的時候&#xff0c;應該及時關閉它們&#xff0c;以…

CYQ.Data 輕量數據層之路 使用篇三曲 MAction 取值賦值(十四)

2019獨角獸企業重金招聘Python工程師標準>>> 上一篇&#xff1a;CYQ.Data 輕量數據層之路 使用篇二曲 MAction 數據查詢(十三&#xff09; 內容概要 本篇繼續上一篇內容&#xff0c;本節介紹所有取值與賦值的相關操作。1&#xff1a;原生&#xff1a;像操作Row一樣…

CYQ.Data 數據框架 發放V1.5版本源碼

2019獨角獸企業重金招聘Python工程師標準>>> 本篇的內容很簡單&#xff0c;就發放V1.5版本源碼&#xff0c;同時補充了所有發布版本的API文檔。 具體相關下載地址見&#xff1a; 秋色園下載中心&#xff1a;http://www.cyqdata.com/download/article-detail-426 如何…

New Bus Route

New Bus Route CodeForces - 792A There are n cities situated along the main road of Berland. Cities are represented by their coordinates — integer numbers a1,?a2,?...,?an. All coordinates are pairwise distinct. It is possible to get from one city to …

愛說說技術原理:與TXT交互及MDataTable對Json的功能擴展(二)

2019獨角獸企業重金招聘Python工程師標準>>> 關于愛說說在技術選型的文章見&#xff1a;"愛說說"技術原理方案的定選思考過程 本篇將講述“愛說說”比較重大的技術問題點及解決手段&#xff1a; 愛說說&#xff1a;http://speak.cyqdata.com/ 雜說幾句&am…

ActiveXObject 安裝

將后綴名為ocx的文件拷貝至目錄 c:\Windows\SysWOW64\。執行如下命令&#xff0c;進行注冊&#xff1a;regsvr32 c:\Windows\SysWOW64\x.ocx轉載于:https://www.cnblogs.com/Currention/p/11024354.html

如何制作VSPackage的安裝程序

2019獨角獸企業重金招聘Python工程師標準>>> 第一步&#xff0c;生成一個REG文件&#xff1a; 收錢進入目錄: C:\Program Files\Microsoft Visual Studio 2008 SDK\VisualStudioIntegration\Tools\Bin 這是SDK的目錄&#xff0c;使用regpkg.exe 命令 命令格式為: …

MyBatis學習總結(1)——MyBatis快速入門

2019獨角獸企業重金招聘Python工程師標準>>> 一、Mybatis介紹 MyBatis是一個支持普通SQL查詢&#xff0c;存儲過程和高級映射的優秀持久層框架。MyBatis消除了幾乎所有的JDBC代碼和參數的手工設置以及對結果集的檢索封裝。MyBatis可以使用簡單的XML或注解用于配置和…

MyEclipse+Tomcat+MAVEN+SVN項目完整環境搭建

2019獨角獸企業重金招聘Python工程師標準>>> 這次換了臺電腦&#xff0c;所以需要重新配置一次項目開發環境&#xff0c;過程中的種種&#xff0c;記錄下來&#xff0c;便于以后再次安裝&#xff0c;同時給大家一個參考。 1.JDK的安裝 首先下載JDK&#xff0c;這個從…