uva10318 Security Panel (dfs+ pruning)

Source: Internet
Author: User

Test instructions

There is a r*c on the matrix to select some lattice for "light" operation, so that the final lattice is "bright" state. All the squares above the initial matrix are dark, from top left to bottom, numbering starts at 1.
Now let's give a 3*3 matrix, as a button rule:
For example:
**.
.*.
*..
That is, you press any button, the button as the center of the 3*3 Matrix, according to this rule, that is, press a button, the button around the position of the *, according to the rule, the state changes (if it is not already on a line is ignored)

Give the r,c a few rows of columns
Then give a fixed 3*3 matrix that represents the rules of the button
Ask at least which buttons are pressed to make all the buttons light.

Ideas:

A deep search solution with a straight line. The status of each button is only two lit and not lit.
But there are at most 2^25 states, or very large.
So there needs to be a pruning, that is, you have traversed to line X, if the X-2 line has a button that is not bright, then exit directly, because a button at most affect it on the line, the last two lines are not bright will never light.

AC Code

#include <cstdio>#include <cstring>using namespace STD;Const intINF =0x3f3f3f3f;Const intDx[] = {-1,-1,-1,0,0,0,1,1,1};Const intDy[] = {-1,0,1,-1,0,1,-1,0,1};Const intN =5;CharStr[n];intpattern[3][3], grid[n][n];intR, C, MINV, ans[ -], pressed[ -];intState[n][n];BOOLCheckintx) {if(X <0)return true; for(inti =0; I <= x; i++) { for(intj =0; J < C; J + +) {if(!state[i][j])return false; }    }return true;}voidFuncintXintY) {intNX, NY; for(inti =0; I <9; i++) {if(pattern[1+dx[i]][1+dy[i]] {NX = X+dx[i], NY = y+dy[i];if(NX <0|| NX >= R | | NY <0|| NY >= C)Continue;        State[nx][ny] =!state[nx][ny]; }    }}voidDfsintXintYintcur) {if(y = = c) {if(x = = R1&& check (x)) {if(Minv > cur) {MINV = cur;memcpy(ANS, pressed,sizeof(ans)); }        }Else if(X < R1&& Check (X-2) {DFS (x+1,0, cur); }return; }//selectPressed[cur] = x*c + y +1;    Func (x, y); DFS (x, y+1, cur+1); Func (x, y);//unselectDFS (x, y+1, cur);}intMain () {//Freopen ("In.txt", "R", stdin);    intCAS =1; while(scanf("%d%d", &r, &c)! = EOF && (R | | c)) {//init        memset(State,0,sizeof(state)); for(inti =0; I <3; i++) {scanf('%s ', &AMP;STR); for(intj =0; J <3; J + +) {Pattern[i][j] = (str[j] = =' * ');        }} MINV = INF; Dfs0,0,0);printf("Case #%d\n", cas++);if(MINV! = INF) {printf("%d", ans[0]); for(inti =1; i < MINV; i++) {printf("%d", Ans[i]); }puts(""); }Else{printf("impossible.\n"); }    }return 0;}

uva10318 Security Panel (dfs+ pruning)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.