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");
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值