UVA 10318 - Security Panel(dfs+剪枝)

来源:互联网 发布:适合iphone的软件 编辑:程序博客网 时间:2024/04/30 20:42

Problem G

Security Panel

Input: standard input

Output: standard output

Time Limit: 2 seconds

Memory Limit: 32 MB

 

Advanced Control Mechanisms (ACM) produces sophisticated electronic locks and security devices.

The company's most recent invention is a panel of illuminated buttons in r rows and c columns. The buttons are numbered left-to-right, top-to-bottom, starting at 1 in the upper-left corner. Each button has two states: lit and unlit. Initially, all buttons are unlit. Pressing a button switches the state of some buttons from lit to unlit (or vice-versa) according to a 3x3 pattern. Pressing a button on a panel applies the pattern centered on that button. To unlock the panel, the buttons must be pressed in such a way so as to light all of them.

For example, consider the following pattern where pressing a button switches the state of the button pressed, as well as the button above and the buttons to the upper and lower left.


 

If we use this pattern on a 2x3 panel, then pressing buttons 25, and 6 will light all the buttons. If pressed in that order, the state changes of the panel are: (Light green means lights are off and vice versa)

 

 

Input

Each input case will begin with the number of rows and columns on the panel, 1 <= r, c <= 5 alone on a line. The next three lines describe how pressing a button will affect the nearby lights. This description consists of a 3x3 character grid, where the character "*"indicates that the light in that position switches state (from lit to unlit or from unlit to lit) while "." means its state remains unchanged.

Input ends with 0 0 alone on a line.


Output

For each input case, output "Case #" followed by the number of the case. If there is no way to turn on all the lights, print "Impossible."If it is possible to turn on the lights, output the buttons to be pressed in increasing order, separated by single space. Output the answer that requires the fewest number of buttons possible to be pressed. If there is more than one correct solution, anyone will do.

 

Sample Input

2 3
**.
.*.
*..
4 5
.*.
***
.*.
2 2
...
.**
...
4 3
*.*
...
..*
0 0

 


题意;给r*c盏灯,和开关方式,求最少按的方式使得所有灯亮起来。

思路:dfs+剪枝,单单dfs需要2^25的复杂度,对于当前按键,是影响不到r-2行之前的灯的,所以如果r-2行有不亮的灯,这种状态便不行。

代码:

#include <stdio.h>#include <string.h>#define INF 0x3f3f3f3fconst int N = 10;const int d[9][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {-1, 1}, {1, -1}, {1, 1}, {-1, -1}, {0, 0}};int r, c, res[N][N], ans[N * N], cas = 0, path[N * N], Min, ansn;char g[N][N];void init() {    Min = INF; ansn = 0;    memset(res, 0, sizeof(res));    for (int i = 0; i < 3; i ++)scanf("%s", g[i]);}void tra(int x, int y, int &num) {    for (int i = 0; i < 9; i ++) {int xx = x + d[i][0];int yy = y + d[i][1];int xxx = 1 + d[i][0];int yyy = 1 + d[i][1];if (xx >= 0 && xx < r && yy >= 0 && yy < c && g[xxx][yyy] == '*') {    if (res[xx][yy]) {res[xx][yy] = 0; num --;    }    else {res[xx][yy] = 1; num ++;    }}    }}bool judge(int x) {    for (int i = 0; i < x; i ++)for (int j = 0; j < c; j ++)    if (!res[i][j]) return false;    return true;}void dfs(int x, int y, int num) {    if (num == r * c) {if (Min > ansn) {    Min = ansn;    for (int i = 0; i < ansn; i ++)path[i] = ans[i];}return;    }    if (x == r) return;    if (y == c) {if(!judge(x))    return;dfs(x + 1, 0, num);return;    }    tra(x, y, num);    ans[ansn++] = x * c + y + 1;    dfs(x, y + 1, num);    ansn--;    tra(x, y, num);    dfs(x, y + 1, num);}void solve() {    printf("Case #%d\n", ++cas);    dfs(0, 0, 0);    if (Min == INF) printf("Impossible.\n");    else {printf("%d", path[0]);for (int i = 1; i < Min; i ++)    printf(" %d", path[i]);printf("\n");    }}int main() {    while (~scanf("%d%d", &r, &c) && r + c) {init();solve();    }    return 0;}

还有一种写法更为快速,尽量不放,这样一旦找到一个正确答案便是最小要按的次数。

#include <stdio.h>#include <string.h>#define INF 0x3f3f3f3fconst int N = 10;const int d[9][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {-1, 1}, {1, -1}, {1, 1}, {-1, -1}, {0, 0}};int r, c, res[N][N], ans[N * N], cas = 0, path[N * N], Min, ansn;char g[N][N];void init() {    Min = INF; ansn = 0;    memset(res, 0, sizeof(res));    for (int i = 0; i < 3; i ++)scanf("%s", g[i]);}void tra(int x, int y, int &num) {    for (int i = 0; i < 9; i ++) {int xx = x + d[i][0];int yy = y + d[i][1];int xxx = 1 + d[i][0];int yyy = 1 + d[i][1];if (xx >= 0 && xx < r && yy >= 0 && yy < c && g[xxx][yyy] == '*') {    if (res[xx][yy]) {res[xx][yy] = 0; num --;    }    else {res[xx][yy] = 1; num ++;    }}    }}bool judge(int x) {    for (int i = 0; i < x; i ++)for (int j = 0; j < c; j ++)    if (!res[i][j]) return false;    return true;}bool dfs(int x, int y, int num) {    if (num == r * c) {printf("%d", ans[0]);for (int i = 1; i < ansn; i ++)    printf(" %d", ans[i]);printf("\n");return true;    }    if (x == r) return false;    if (y == c) {if(!judge(x))    return false;if (dfs(x + 1, 0, num)) return true;return false;    }    if (dfs(x, y + 1, num)) return true;    tra(x, y, num);    ans[ansn++] = x * c + y + 1;    if (dfs(x, y + 1, num)) return true;    ansn--;    tra(x, y, num);    return false;}void solve() {    printf("Case #%d\n", ++cas);    if(!dfs(0, 0, 0)) printf("Impossible.\n");}int main() {    while (~scanf("%d%d", &r, &c) && r + c) {init();solve();    }    return 0;}



1 0