UVa387 - A Puzzling Problem

原创 2012年03月26日 18:11:35
#include <stdio.h>
#include <string.h>

#define MAX 16
#define N 4

typedef struct node {
    int shape[N][N];
    int startx, starty;
    int row, col;
} node;

node inode[MAX];
int shapenum;
int vis[N][N];

int dfs(int cur, int v[][N]);
int is_vis(int v[][N], int i, int j, int m, int n, int startx, int starty);

int main()
{
    int n;
    int row, col;
    int i, j;
    char ch;
    int first;
    int iCase = 0;

#ifndef ONLINE_JUDGE
    freopen("d:\\UVa\\uva_in.txt", "r", stdin);
    /*freopen("d:\\UVa\\uva_out.txt", "w", stdout);*/
#endif

    while (scanf("%d", &n) && n != 0) {
        shapenum = 0;
        memset(vis, 0, sizeof(vis));
        memset(inode, 0, sizeof(inode));
        while (n--) {
            scanf("%d%d", &row, &col);
            inode[shapenum].row = row;
            inode[shapenum].col = col;
            getchar();
            first = 1;
            for (i = 0; i < row; i++) {
                for (j = 0; j < col; j++) {
                    scanf("%c", &ch);
                    if (ch == '1') {
                        inode[shapenum].shape[i][j] = 1;
                        if (first) {
                            inode[shapenum].startx = i;
                            inode[shapenum].starty = j;
                            first = 0;
                        }
                    } else
                        inode[shapenum].shape[i][j] = 0;
                }
                getchar();
            }

#ifndef ONLINE_JUDGE
            for (i = 0; i < row; i++) {
                for (j = 0; j < col; j++)
                    printf("%d ", inode[shapenum].shape[i][j]);
                printf("\n");
            }
            printf("\n");
#endif
            shapenum++;
        }
        /*printf("shapenum=%d\n", shapenum);*/
        if (iCase++)
        	printf("\n");
        if (dfs(0, vis))
            ;
        else
            printf("No solution possible\n");
    }

    return 0;
}

int dfs(int cur, int v[][N])
{
    int i, j;
    int temp[N][N];
    int m, n;
    int ok;
    int row, col;
    int startx, starty;

    if (cur == shapenum) {
        for (i = 0; i < N; i++) {
            for (j = 0; j < N; j++) {
                if (!v[i][j])
                    return 0;
            }
        }

        for (i = 0; i < N; i++) {
            for (j = 0; j < N; j++) {
                printf("%d", v[i][j]);
            }
            printf("\n");
        }
        return 1;
    }

    row = inode[cur].row;
    col = inode[cur].col;
    startx = inode[cur].startx;
    starty = inode[cur].starty;
    /*memcpy(temp, v, sizeof(int) * N * N);*/



    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) {
            memcpy(temp, v, sizeof(int) * N * N);
            if (!temp[i][j]) {
                ok = 1;
                for (m = 0; m < row && ok; m++) {
                    for (n = 0; n < col && ok; n++) {
                        if (inode[cur].shape[m][n]) {
                            if (is_vis(temp, i, j, m, n, startx, starty) == 0)
                                ok = 0;
                        }
                    }
                }

                if (ok) {
                    for (m = 0; m < row; m++) {
                        for (n = 0; n < col; n++) {
                            if (inode[cur].shape[m][n])
                                temp[i + m - startx][j + n - starty] = cur + 1;
                        }
                    }
#ifndef ONLINE_JUDGE
                    for (i = 0; i < N; i++) {
                        for (j = 0; j < N; j++) {
                            printf("%d ", temp[i][j]);
                        }
                        printf("\n");
                    }
                    printf("\n");
#endif

                    if (dfs(cur + 1, temp))
                        return 1;

#ifndef ONLINE_JUDGE
                    for (m = 0; m < N; m++) {
                        for (n = 0; n < N; n++) {
                            if (temp[i + m - startx][j + n - starty] == cur + 1)
                                temp[m][n] = 0;;
                        }
                    }
#endif

                }

            }
        }
    }
    return 0;

}

int is_vis(int v[][N], int i, int j, int m, int n, int startx, int starty)
{
    if (i + m - startx >= N || i + m - startx < 0 || j + n - starty >= N || j + n - starty < 0 || v[i + m - startx][j + n - starty])
        return 0;
    return 1;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

UVA - 387 A Puzzling Problem(回溯)

A Puzzling Problem  The goal of this problem is to write a program which will take from 1...

uva 387 A Puzzling Problem

题目地址: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&proble...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

uva 387 - A Puzzling Problem

A Puzzling Problem  The goal of this problem is to write a program which will take from 1...

uva 387(回溯)

题意:给出了一些b 题解: #include #include const int N = 20; char g[N][5][5]; int n, m[N][2], flag, vis[5]...

UVA387

题目的意思就是给你一些碎片,这些sui

Uva 307木棍、387谜题、10160服务站总结

Uva 307木棍、387谜题、10160服务站总结,代码加解释

Uva101—— The Blocks Problem

#include #include #include #include using namespace std; const int maxn = 30; int n; vector pil...

UVA 101 The Blocks Problem

比预期慢了很多 现在才将刘汝佳的书看到第五章,各种实用stl函数不仅让我有种收获利器的感觉,也毫无系统学了一些用法,都是满满的新鲜干货,对于我现在来说。比如这个vector 的用法,不定长数组。 若...

UVA 10026 Shoemaker's Problem

Shoemaker's Problem Shoemaker has N jobs (orders from customers) which he must make. Shoemaker...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)