UVa 387 - A Puzzling Problem 解題報告(暴力)

 A Puzzling Problem 

The goal of this problem is to write a program which will take from 1 to 5puzzle pieces such asthose shown below and arrange them, if possible, to form a square.An example set of pieces is shown here.

The pieces cannot be rotated or flipped from their original orientationin an attempt to form asquare from the set. All of the pieces must be used to form thesquare. There may be more thanone possible solution for a set of pieces, and not every arrangementwill work even with a set forwhich a solution can be found. Examples using the above set ofpieces are shown here.

Input

The input file for this program contains several puzzles (i.e. sets ofpuzzle pieces) to be solved.The first line of the file is the number of pieces in the first puzzle.Each piece is then specified bylisting a single line with two integers, the number of rows andcolumns in the piece, followed byone or more lines which specify the shape of the piece.The shape specification consists of `0' and`1' characters, with the `1' characters indicating the solid shapeof the puzzle (the `0' charactersare merely placeholders). For example, piece `A' above would bespecified as follows:

2 3
111
101

The pieces should be numbered by the order they are encountered in thepuzzle. That is, the firstpiece in a puzzle is piece #1, the next is piece #2, etc. All piecesmay be assumed to be valid and no larger than 4 rows by 4 columns.

The line following the final line of the last piece contains thenumber of pieces in the next puzzle,again followed by the puzzle pieces and so on. The end of the input fileis indicated by a zero in place of the number of puzzle pieces.

Output

Your program should report a solution, if one is possible, in theformat shown by the examplesbelow. A 4-row by 4-column square should be created, with each pieceoccupying its location inthe solution. The solid portions of piece #1 should be replacedwith `1' characters, of piece #2with `2' characters, etc. The solutions for each puzzle should beseparated by a single blank line.

If there are multiple solutions, any of them is acceptable. For puzzles which have no possible solution simplyreport ``No solution possible''.

Sample Input

4
2 3
111
101
4 2
01
01
11
01
2 1
1
1
3 2
10
10
11
4
1 4
1111
1 4
1111
1 4
1111
2 3
111
001
5
2 2
11
11
2 3
111
100
3 2
11
01
01
1 3
111
1 1
1
0

Sample Output

1112
1412
3422
3442

No solution possible

1133
1153
2223
2444


    解題報告:暴力枚舉每個塊所在的位置,如果與當前狀態無重疊,那麼繼續枚舉下去,全部枚舉完成就說明成功了。代碼如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <iomanip>
using namespace std;
#define ff(i, n) for(int i=0;i<(n);i++)
#define fff(i, n, m) for(int i=(n);i<=(m);i++)
#define dff(i, n, m) for(int i=(n);i>=(m);i--)
#define bit(n) (1LL<<(n))
typedef long long LL;
typedef unsigned long long ULL;
const LL inf=1e15;
void work();
int main()
{
#ifdef ACM
    freopen("in.txt", "r", stdin);
#endif // ACM
    work();
}

/***************************************************/

bool ans;
int n;
int maze[8][8];

struct Node
{
    int box[4][4];
} node[10];

void dfs(int c)
{
    if(c == n)
    {
        ans = true;
        return;
    }

    fff(x, 1, 4) fff(y, 1, 4)
    {
        bool flag = true;
        ff(i, 4) ff(j, 4) if(node[c].box[i][j])
        {
            if(maze[x+i][y+j])
                flag = false, i = 4, j = 4;
            else
                maze[x+i][y+j] = c + 1;
        }

        if(flag == false)
        {
            fff(i, 1, 4) fff(j, 1, 4) if(maze[i][j] == c+1)
                maze[i][j] = 0;
            continue;
        }

        dfs(c+1);
        if(ans) return;

        fff(i, 1, 4) fff(j, 1, 4) if(maze[i][j] == c+1)
            maze[i][j] = 0;
    }
}

void work()
{
    memset(maze, -1, sizeof(maze));

    int first = 0;
    while(scanf("%d", &n) == 1 && n)
    {
        if(first++) puts("");

        fff(i, 1, 4) fff(j, 1, 4) maze[i][j] = 0;

        char str[11];
        int x, y, tmp, count = 0;
        ff(i, n)
        {
            memset(node[i].box, 0, sizeof(node[i].box));

            scanf("%d%d", &x, &y);
            ff(xx, x)
            {
                scanf("%s", str);
                ff(yy, y) if(str[yy] == '1')
                    node[i].box[xx][yy] = 1, count ++;
            }
        }

        ans = false;
        if(count == 16)
            dfs(0);

        if(ans)
            fff(i, 1, 4)
            {
                fff(j, 1, 4) printf("%d", maze[i][j]);
                puts("");
            }
        else
            puts("No solution possible");
    }
}

    使用位運算記錄地圖和方塊更方便一點,效率也更高。代碼如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <iomanip>
using namespace std;
#define ff(i, n) for(int i=0;i<(n);i++)
#define fff(i, n, m) for(int i=(n);i<=(m);i++)
#define dff(i, n, m) for(int i=(n);i>=(m);i--)
#define bit(n) (1LL<<(n))
typedef long long LL;
typedef unsigned long long ULL;
const LL inf=1e15;
void work();
int main()
{
#ifdef ACM
    freopen("input.in", "r", stdin);
#endif // ACM
    work();
}

/***************************************************/

bool ans;
LL maze;
int n;
LL box[10];
int pos[10];
int res[4][4];

void dfs(int c)
{
    if(c == n)
    {
        ans = true;
        return;
    }

    ff(i, 32) if((maze & (box[c] << i)) == 0)
    {
        maze ^= (box[c] << i);
        pos[c] = i;
        dfs(c+1);
        if(ans) return;
        maze ^= (box[c] << i);
    }
}

void work()
{
    int first = 0;
    while(scanf("%d", &n) == 1 && n)
    {
        if(first++) puts("");

        maze = -1;
        maze ^= 0x0F0F0F0F;

        ff(nn, n)
        {
            int x, y;
            scanf("%d%d", &x, &y);
            box[nn] = 0;

            ff(xx, x)
            {
                char str[10];
                scanf("%s", str);
                ff(yy, y) if(str[yy] == '1')
                    box[nn] |= bit(xx*8 + yy);
            }
        }

        ans = false;
        dfs(0);

        if(ans && maze == -1)
        {
            ff(i, n) ff(x, 4) ff(y, 4) if(bit(x*8 + y) & (box[i] << pos[i]))
                res[x][y] = i + 1;
            ff(x, 4)
            {
                ff(y, 4) printf("%d", res[x][y]);
                puts("");
            }
        }
        else
        {
            puts("No solution possible");
        }
    }
}


發佈了227 篇原創文章 · 獲贊 13 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章