UVA - 12113 Overlapping Squares(dfs+回溯)

题目:

给定一个4*4的棋盘和棋盘上所呈现出来的纸张边缘,问用不超过6张2*2的纸能否摆出这样的形状。

思路:

dfs纸的张数,每一张中枚举这张纸左上角这个点的位置,暴力解题就可以了。

这个题的覆盖太恶心了,很容易搞混~~~(因为搞混一直TLE+WA…………)

代码:

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define MAX 1000000000
#define mod 1000000007
#define FRE() freopen("in.txt","r",stdin)
#define FRO() freopen("out.txt","w",stdout)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;//first-距离 second-编号
const int maxn = 8;
int ans[6][10],mp[5][10];
char str[5][10];

bool judge(){
    for(int r=0; r<5; r++){
        for(int c=0; c<9; c++){
            if(ans[r][c]!=mp[r][c]){
                return false;
            }
        }
    }
    return true;
}

void copyArray(int a[5][10],int b[5][10]){
    for(int i=0; i<5; i++){
        for(int j=0; j<9; j++){
            a[i][j] = b[i][j];
        }
    }
}

void putPapper(int x,int y){//-95 |124
    mp[x][y+1]=mp[x][y+3]=2;
    mp[x+2][y+1]=mp[x+2][y+3]=2;

    //mp[x][y]=mp[x][y+2]=mp[x][y+4]=32;
    mp[x+2][y+2]=0;
    mp[x+1][y+1]=mp[x+1][y+2]=mp[x+1][y+3]=0;

    mp[x+1][y]=mp[x+1][y+4]=1;
    mp[x+2][y]=mp[x+2][y+4]=1;
}

bool dfs(int deep){
    if(deep>6) return false;
    for(int i=0; i<3; i++){
        for(int j=0; j<=4; j+=2){
            int temp[5][10];
            copyArray(temp,mp);
            putPapper(i,j);
            if(judge())return true;
            if(dfs(deep+1))return true;
            copyArray(mp,temp);
        }
    }
    return false;
}

void check(){
    for(int i=0; i<5; i++){
        for(int j=0; j<9; j++){
            printf("%3d",ans[i][j]);
        }
        printf("\n");
    }
    printf("\n\n\n\n");
}

int main(){
    //FRE();
    int kase = 0;
    while(gets(str[0])&&str[0][0]!=‘0‘){
        memset(mp,0,sizeof(mp));
        memset(ans,0,sizeof(ans));
        for(int i=1; i<5; i++){
            gets(str[i]);
        }
        for(int i=0; i<5; i++){
            for(int j=0; j<9; j++){
                if(str[i][j]==‘_‘) ans[i][j]=2;
                else if(str[i][j]==‘|‘) ans[i][j]=1;
            }
        }
        //check();

        if(dfs(1)){
            printf("Case %d: Yes\n",++kase);
        }else{
            printf("Case %d: No\n",++kase);
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/sykline/p/10414464.html

时间: 2024-11-09 12:11:07

UVA - 12113 Overlapping Squares(dfs+回溯)的相关文章

UVA 12113 Overlapping Squares

题意: 总共有6个2*2的正方形,判断是否能够成所给的形状. 思路: 一个正方形总共有9种摆放方式,对于整个地图来说摆放方式总共有2的9次方种摆放方式.然后将地图用9*5的数组表示,正方形的位置用其8个边的下标和4个中空的下标表示. 代码: #include <iostream>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;int bian[9][8]={

uva 11218 KTV(DFS+回溯)

uva 11218 KTV One song is extremely popular recently, so you and your friends decided to sing it in KTV. The song has 3 characters, so exactly 3 people should sing together each time (yes, there are 3 microphones in the room). There are exactly 9 peo

UVa 129 Krypton Factor (DFS &amp;&amp; 回溯)

题意 : 如果一个字符串包含两个相邻的重复子串,则称它是"容易的串",其他串称为"困难的 串".例如,BB.ABCDACABCAB.ABCDABCD都是容易的串,而D.DC.ABDAB. CBABCBA都是困难的串.程序从输入中读取多行数据,每行包括两个整数n和L(即按此顺序给出),其中n > 0,L的范围是1 ≤ L ≤ 26.根据这些输入,程序要按照字母表升序打印出第n个"hard"字串(由字母表中的前L个字母构成),并在接下来的一行打

UVA-12113 Overlapping Squares (回溯+暴力)

题目大意:问能不能用不超过6张2x2的方纸在4x4的方格中摆出给定的图形? 题目分析:暴力枚举出P(9,6)种(最坏情况)方案即可. 代码如下: # include<iostream> # include<cstdio> # include<cstring> # include<algorithm> using namespace std; int vis[10]; char mp[8][15],p1[8][15]; bool judge() { for(i

UVA 23 Out of 5(DFS+回溯)

Problem I 23 Out of 5 Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Your task is to write a program that can decide whether you can find an arithmetic expression consisting of five given numbers (1<=i<=5) tha

UVA How Big Is It? (DFS+回溯)

 How Big Is It?  Ian's going to California, and he has to pack his things, including his collection of circles. Given a set of circles, your program must find the smallest rectangular box in which they fit. All circles must touch the bottom of the bo

POJ2488-A Knight&#39;s Journey(DFS+回溯)

题目链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36695   Accepted: 12462 Description Background The knight is getting bored of seeing the same black and white squares again and again

POJ 2907 Collecting Beepers (DFS+回溯)

Description Karel is a robot who lives in a rectangular coordinate system where each place is designated by a set of integer coordinates (x and y). Your job is to design a program that will help Karel pick up a number of beepers that are placed in he

数独问题(DFS回溯)

Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure. In some of the cells are written decimal digits from 1 to 9. The other cells are empty. The goal is to fill the empty c