惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Microsoft Azure Blog
Microsoft Azure Blog
大猫的无限游戏
大猫的无限游戏
月光博客
月光博客
V
V2EX
PCI Perspectives
PCI Perspectives
Latest news
Latest news
博客园 - 三生石上(FineUI控件)
C
CERT Recently Published Vulnerability Notes
W
WeLiveSecurity
Last Week in AI
Last Week in AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
P
Palo Alto Networks Blog
T
The Exploit Database - CXSecurity.com
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
WordPress大学
WordPress大学
V
Vulnerabilities – Threatpost
H
Heimdal Security Blog
Attack and Defense Labs
Attack and Defense Labs
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News: Ask HN
Hacker News: Ask HN
博客园 - 叶小钗
V
Visual Studio Blog
Jina AI
Jina AI
P
Proofpoint News Feed
罗磊的独立博客
SecWiki News
SecWiki News
J
Java Code Geeks
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
LINUX DO - 热门话题
Security Archives - TechRepublic
Security Archives - TechRepublic
The Hacker News
The Hacker News
Hugging Face - Blog
Hugging Face - Blog
N
News and Events Feed by Topic
NISL@THU
NISL@THU
T
Tailwind CSS Blog
T
Tenable Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Recent Announcements
Recent Announcements
H
Hacker News: Front Page
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
Tor Project blog
宝玉的分享
宝玉的分享
Help Net Security
Help Net Security
S
Security Affairs
Microsoft Security Blog
Microsoft Security Blog
Google DeepMind News
Google DeepMind News
F
Fortinet All Blogs
G
GRAHAM CLULEY

博客园 - Mose

CS学习 NOI2002_ Galaxy银河英雄传说86 SGU 223 little kings BSOJ2772 状压DP BZOJ 3132(上帝造题的七分钟-树状数组求和+2D逆求和数组) 埃及分数 [usaco]2013-jan Liars and Truth Tellers 真假奶牛 【区间DP】codevs3657 括号序列题解 ubuntu安装php-curl拓展 MIT挑战(如何在12个月内自学完成MIT计算机科学的33门课程|内附MIT公开课程资源和学习顺序 大白话Docker入门(一) Hexo博客搭建全解 代码查重工具sim virtual judge 本地部署方案 POJ题目分类推荐 (很好很有层次感) 解决Ubuntu下Sublime Text 3无法输入中文 [pascal入门]数组 [codecademy]fonts in css [codecademy]css Ubuntu录制gif动态图
[Elite 2008 Dec USACO]Jigsaw Puzzles
Mose · 2017-05-19 · via 博客园 - Mose
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define Up num[0]
#define Right num[1]
#define Down num[2]
#define Left num[3]

char ca,cb,cc,cd;
int r,c,rc;
bool v[110];

class Jigsaw
{
public:
    int serialNumber;
    char num[4];
public:
    void readIn()
    {
        scanf("%d%*c%c%*c%c%*c%c%*c%c%*c",&serialNumber,num,num+1,num+2,num+3);
    }
    void printOut()
    {
        printf("%d %c %c %c %c\n",serialNumber,num[0],num[1],num[2],num[3]);
    }
    int checkCorner()
    {
        int i;
        for (i=0; i<4; i++)
        {
            if (num[i] == '0' && num[(i+1)%4] == '0')
                return i;
        }
        return -1;
    }
    int checkSuit2c()
    {
        int i;
        for (i=0; i<4; i++)
        {
            if (num[i] == ca && num[(i+1)%4] == cb)
                return i;
        }
        return -1;
    }
    int checkSuit3c()
    {
        int i;
        for (i=0; i<4; i++)
        {
            if (num[i] == ca && num[(i+1)%4] == cb && num[(i+2)%4] == cc)
                return i;
        }
        return -1;
    }
    int checkSuit4c()
    {
        int i;
        for (i=0; i<4; i++)
        {
            if (num[i] == ca && num[(i+1)%4] == cb && num[(i+3)%4] == cc)
                return i;
        }
        return -1;
    }
};
Jigsaw js[110];
Jigsaw tb[11][11];
bool check_and_rotate(int x,int y,int d)
{
    int t;
    if (x == 0 && y == 0)
    {
        t=js[d].checkCorner();
        if (t == -1)
            return false;
        tb[x][y].serialNumber=js[d].serialNumber;
        tb[x][y].Left=js[d].num[t];
        tb[x][y].Up=js[d].num[(t+1)%4];
        tb[x][y].Right=js[d].num[(t+2)%4];
        tb[x][y].Down=js[d].num[(t+3)%4];
        return true;
    }
    if (x == 0)
        cb='0';
    else
        cb=tb[x-1][y].Down;
    if (y == 0)
        ca='0';
    else
        ca=tb[x][y-1].Right;
    if (y == c-1)
    {
        cc='0';
        t=js[d].checkSuit3c();
        if (t == -1)
            return false;
        tb[x][y].serialNumber=js[d].serialNumber;
        tb[x][y].Left=js[d].num[t];
        tb[x][y].Up=js[d].num[(t+1)%4];
        tb[x][y].Right=js[d].num[(t+2)%4];
        tb[x][y].Down=js[d].num[(t+3)%4];
        return true;
    }
    else if (x == r-1)
    {
        cc='0';
        t=js[d].checkSuit4c();
        if (t == -1)
            return false;
        tb[x][y].serialNumber=js[d].serialNumber;
        tb[x][y].Left=js[d].num[t];
        tb[x][y].Up=js[d].num[(t+1)%4];
        tb[x][y].Right=js[d].num[(t+2)%4];
        tb[x][y].Down=js[d].num[(t+3)%4];
        return true;
    }
    else
    {
        t=js[d].checkSuit2c();
        if (t == -1)
            return false;
        tb[x][y].serialNumber=js[d].serialNumber;
        tb[x][y].Left=js[d].num[t];
        tb[x][y].Up=js[d].num[(t+1)%4];
        tb[x][y].Right=js[d].num[(t+2)%4];
        tb[x][y].Down=js[d].num[(t+3)%4];
        return true;
    }
    return false;
}
bool DFS(int x,int y)
{
    int i,j;
    if (y == c)
    {
        x++;
        y=0;
        if (x == r)
        {
            for (i=0; i<r; i++)
            {
                for (j=0; j<c; j++)
                {
                    tb[i][j].printOut();
                }
            }
            return true;
        }
        return DFS(x,y);
    }
    for (i=0; i<rc; i++)
    {
        if (v[i] == true)
            continue;
        if (check_and_rotate(x,y,i) == true)
        {
            v[i]=true;
            if (DFS(x,y+1) == true)
                return true;
            v[i]=false;
        }
    }
    return false;
}
int main()
{
    int i,j;
    scanf("%d%d",&r,&c);
    rc=r*c;
    for (i=0; i<rc; i++)
    {
        js[i].readIn();
        v[i]=false;
    }
    DFS(0,0);
    return 0;
}

USACO Elite 2008 December Competition Silver

题意:给你一些正方形的拼图碎块,每个碎块有四个边,每边都有一个记号,分别是小写字母a-z。其中没有标记的边,也就是边界的边用'0'标记。题目给定一个矩阵,把这些碎块放进去,要求每个边相接的记号必须一样,边界上的边必须是'0'。

解法:DFS。这个操作起来还是挺麻烦的,做处理的时候要小心一些。