Paint on a Wall
Time limit:10000/5000 MS (java/others) Memory limit:65768/65768 K (java/others)
Total submission (s): 830 Accepted Submission (s): 325
Problem Descriptionannie wants to paint his wall to an expected pattern. The wall can represented as a 2*n grid and each grid would be is painted only one color. Annie has a brush which could paint a rectangular area of the wall at a single step. The paint could be overlap and the newly painted color would replace the older one.
For a given pattern of the wall, your task was to help Annie find out the minimum possible number of painting steps. You can assume this wall remains unpainted until Annie paint some colors on it.
Inputthere is multiple test cases in the input. The first line contains the number of test cases.
For each test case, the first line contains the only integer n indicating the length of the wall. (1 <= n <= 8)
Lines follow, denoting the expected pattern of the wall. The color is represented by a.
See the sample input for further details.
Outputfor each test case, output is only one integer denoting the minimum number of steps.
Sample INPUT33ABACBC 3BAACCB 3BBBBAB
Sample outputcase #1:3Case #2:3Case #3:2 n <= 8.. Very classic search. Pressure + BFS, using binary to indicate whether the target color is reached. You can find a property that does not find a point that does not reach the target color can be directly to it as a vertex to the left, to the right to construct a new state is relatively rotten, ran nearly 3 s
#include <bits/stdc++.h>using namespacestd; typedef pair<int,int>PII;#defineX First#defineY SecondConst intN =1<< -;intN;strings, S2;structNode {intW, St; Node () {} node (intWintSt): W (W), St (ST) {}BOOL operator< (ConstNode &a)Const { returnW >A.W; }};voidUpdateint&st,intIintJBOOLtag) { if(St & (1<<J)) && s[j]! = S[i]) St ^= (1<<j); if( ! (St & (1<<J)) && s[j] = = S[i]) St |= (1<<j); if(!tag)return ; if(St & (1<< ((j+n)% (2*n))) && s[(j+n)% (2*N)]! = S[i]) St ^= (1<< ((j+n)% (2*n))) ; if( ! (St & (1<< (j+n)% (2*n)) && s[(j+n)% (2*n)] [= S[i]) St |= (1<< ((j+n)% (2*n )));//cout << J << ' << (j+n)% (2*n) << Endl;}voidShowintSt) { for(inti =0; I < n; ++i)if(st& (1<<i)) cout <<'1';Elsecout <<'0'; cout <<Endl; for(inti = n; I <2*n; ++i)if(st& (1<<i)) cout <<'1';Elsecout <<'0'; cout <<Endl;}intDis[n], all;voidBFs () {priority_queue<node>que; Que.push (Node (0,0) ) ; memset (DIS,0x3f,sizeofdis); dis[0] =0 ;//cout << all << Endl; while( !Que.empty ()) { intUW = Que.top (). W, ust =Que.top (). St; Que.pop (); if(Dis[ust] < UW)Continue ;//cout << uw << Endl; show (UST); cout << Endl; if(ust = = All)return ; intVW = UW +1, VST, Vst2; for(inti =0; I < n; ++i)if( ! (ust& (1<<i)) {VST= ust, Vst2 =ust; for(intj = i; J < N; ++J) {// Single LineUpdate (VST, I, J,false ); Update (Vst2, I, J,true ); if(VW <Dis[vst]) {//cout << i << ' << vst << Endl;//Show (VST); cout << Endl;Dis[vst] =VW; Que.push (VW, VST); } if(VW <Dis[vst2]) {//Show (VST2); cout << Endl;DIS[VST2] =VW; Que.push (VW, VST2); }} VST= ust, Vst2 =ust; for(intj = i; J >=0; --j) {Update (VST, I, J,false ); Update (Vst2, I, J,true ); if(VW <Dis[vst]) {//Show (VST); cout << Endl;Dis[vst] =VW; Que.push (VW, VST); } if(VW <Dis[vst2]) {//Show (VST2); cout << Endl;DIS[VST2] =VW; Que.push (VW, VST2); } } } for(inti = n; I <2N ++i)if( ! (ust& (1<<i)) {VST= ust, Vst2 =ust; for(intj = i; J <2N ++J) {// Single LineUpdate (VST, I, J,false ); Update (Vst2, I, J,true ); if(VW <Dis[vst]) {//Show (VST); cout << Endl;Dis[vst] =VW; Que.push (VW, VST); } if(VW <Dis[vst2]) {//Show (VST2); cout << Endl;DIS[VST2] =VW; Que.push (VW, VST2); }} VST= ust, Vst2 =ust; for(intj = i; J >= N; --j) {Update (VST, I, J,false ); Update (Vst2, I, J,true ); if(VW <Dis[vst]) {//Show (VST); cout << Endl;Dis[vst] =VW; Que.push (VW, VST); } if(VW <Dis[vst2]) {//Show (VST2); cout << Endl;DIS[VST2] =VW; Que.push (VW, VST2); } } } }}intRun () {int_, cas =1; CIN >> _ ; while( _--) {cout<<"Case #"<< cas++ <<": " ; CIN>> N >> S >>S2; S+=S2; All= (1<< (n2))-1; BFS ();//cout << dis[45] << Endl;cout << Dis[all] <<Endl; } return 0 ;}intMain () {//freopen ("In.txt", "R", stdin);Ios::sync_with_stdio (0); returnRun ();}
View Code
HDU 4012 Paint on a Wall (shaped pressure +bfs)