24h購物| | PChome| 登入
2013-08-23 20:30:31| 人氣4,415| 回應0 | 上一篇 | 下一篇

[UVA][搜索bitmask] 10318 - Security Panel

推薦 0 收藏 0 轉貼0 訂閱站台

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 2, 5, 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

 

Sample Output

Case #1
2 5 6
Case #2
2 3 4 7 9 12 14 17 18 19 
Case #3
1 3 
Case #4
Impossible.

(The Decider Contest, Source: University of Alberta Local Contest)

題目類似於關燈問題,但是關燈問題的影響限定十字。

而這題給定影響的情況,問有沒有可能從全關變成全開。

首先最大只有 5x5,考慮將狀態壓縮成一個 int,之後的反轉電燈則使用 XOR 運算即可。

同時預處理每一格壓下的遮罩,純搜索可能會高達 O(2^25),

依序窮舉時,前兩排的燈必然已經全開了,藉此當作剪枝。


#include <stdio.h>
#include <algorithm>
using namespace std;
int mark[10][10];
int row[10];
int n, m;
int ch[105], found;
void dfs(int x, int y, int state, int step) {
    if(found)   return;
    if(y == m) {
        if(x) {
            if((state&row[x-1]) != row[x-1])
                return;
        }
        x++, y = 0;
    }
    int i, j;
    /*for(i = 0; i < n; i++, puts(""))
        for(j = 0; j < m; j++)
            printf("%d", (state>>(i*5+j))&1);
    printf("-------%d %d %d\n", x, y, step);*/
    if(x == n) {
        if((state&row[x-1]) != row[x-1])
            return;
        found = 1;
        for(i = 0; i < step; i++) {
            if(i)   putchar(' ');
            printf("%d", ch[i]+1);
        }
        puts("");
        return;
    }
    dfs(x, y+1, state, step);
    ch[step] = x*m+y;
    dfs(x, y+1, state^mark[x][y], step+1);
}
int main() {
    /*freopen("in.txt","r+t",stdin);
    freopen("out2.txt","w+t",stdout);*/
    char g[5][5];
    int i, j, k, p, q, r;
    int a, b;
    int cases = 0;
    while(scanf("%d %d", &n, &m) == 2 && n) {
        for(i = 0; i < 3; i++)
            scanf("%s", g[i]);
        int x, y;
        for(i = 0; i < n; i++) {
            for(j = 0; j < m; j++) {
                int &val = mark[i][j];
                val = 0;
                for(p = 0, a = -1; p < 3; p++, a++) {
                    for(q = 0, b = -1; q < 3; q++, b++) {
                        x = i+a, y = j+b;
                        if(x < 0 || y < 0 || x >= n || y >= m)
                            continue;
                        if(g[p][q] == '*')
                            val |= 1<<(x*5+y);
                    }
                }
            }
        }
        /*for(i = 0; i < n; i++, puts(""))
            for(j = 0; j < m; j++)
                printf("%d ", mark[i][j]);*/
        for(i = 0; i < n; i++) {
            row[i] = 0;
            for(j = 0; j < m; j++)
                row[i] |= 1<<(i*5+j);
        }
        found = 0;
        printf("Case #%d\n", ++cases);
        dfs(0, 0, 0, 0);
        if(found == 0)
            puts("Impossible.");
    }
    return 0;
}
/*
2 3
**.
.*.
*..
4 5
.*.
***
.*.
2 2
...
.**
...
4 3
*.*
...
..*
0 0
*/


台長: Morris
人氣(4,415) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA] 11065 - A Gentlemen's Agreement
此分類上一篇:[UVA][dp] 1052 - Bit Compressor

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文