UVa 387 - A Puzzling Problem

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=109&page=show_problem&problem=323

题目描述:给定几个图形,问能否拼成一个4*4的矩形

分析:这个题虽然放在难回溯里,但因为没有旋转翻转之类的操作,所以不是很难。一行一行的放就行了。

 

  1 #include <cstdio>

  2 #include <cstring>

  3 

  4 const int MAXN = 10;

  5 

  6 struct piece

  7 {

  8     int r, c;

  9     char tu[MAXN][MAXN];

 10 };

 11 

 12 piece P[210];

 13 char map[MAXN][MAXN];

 14 bool vis[210];

 15 int n;

 16 

 17 void putdown( int cur, int x, int y )

 18 {

 19     for ( int i = 0; i < P[cur].r; ++i )

 20         for ( int j = 0; j < P[cur].c; ++j )

 21         {

 22             if ( P[cur].tu[i][j] - '0' )

 23                 map[x + i][y + j] = cur + 1 + '0';

 24         }

 25     return;

 26 }

 27 

 28 void clear( int cur, int x, int y )

 29 {

 30     for ( int i = 0; i < P[cur].r; ++i )

 31         for ( int j = 0; j < P[cur].c; ++j )

 32         {

 33             if ( P[cur].tu[i][j] - '0' )

 34                 map[x + i][y + j] = '*';

 35         }

 36     return;

 37 }

 38 

 39 bool check( int cur, int x, int y )

 40 {

 41     for ( int i = 0; i < P[cur].r; ++i )

 42         for ( int j = 0; j < P[cur].c; ++j )

 43         {

 44             if ( P[cur].tu[i][j] - '0' )

 45             {

 46                 if ( map[x + i][y + j] != '*' )

 47                     return false;

 48 

 49                 if ( x + i >= 4 || y + j >= 4 )

 50                     return false;

 51             }

 52         }

 53     return true;

 54 }

 55 

 56 void Print()

 57 {

 58     for ( int i = 0; i < 4; ++i )

 59     {

 60         for ( int j = 0; j < 4; ++j )

 61             putchar( map[i][j] );

 62         putchar('\n');

 63     }

 64     return;

 65 }

 66 

 67 bool DFS( int cur )

 68 {

 69     if ( cur == n )

 70     {

 71         for ( int i = 0; i < 4; ++i )

 72             for ( int j = 0; j < 4; ++j )

 73                 if ( map[i][j] == '*' ) return false;

 74         return true;

 75     }

 76 

 77     for ( int x = 0; x < 4; ++x )

 78         for ( int y = 0; y < 4; ++y )

 79         {

 80             for ( int i = 0; i < n; ++i )

 81                 if ( !vis[i] )

 82                 {

 83 //                    printf("%d %d %d\n", i, x, y);

 84 //                    Print();

 85                     vis[i] = true;

 86                     if ( check( cur, x, y ) )

 87                     {

 88                         putdown( cur, x, y );

 89                         if ( DFS( cur + 1 ) ) return true;

 90                         clear( cur, x, y );

 91                     }

 92                     vis[i] = false;

 93                 }

 94         }

 95 

 96     return false;

 97 }

 98 

 99 int main()

100 {

101 //    freopen( "s.out", "w", stdout );

102     bool notfirst = false;

103     while ( scanf( "%d", &n ), n )

104     {

105         for ( int i = 0; i < n; ++i )

106         {

107             scanf( "%d%d", &P[i].r, &P[i].c );

108             for ( int j = 0; j < P[i].r; ++j )

109                 scanf( "%s", P[i].tu[j] );

110         }

111 

112         memset( vis, false, sizeof(vis) );

113         memset( map, '*', sizeof(map) );

114 

115         if ( notfirst ) puts("");

116 

117         if ( DFS( 0 ) ) Print();

118         else puts("No solution possible");

119         notfirst = true;

120     }

121     return 0;

122 }

你可能感兴趣的:(uva)