题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=79

解题报告:

思路简单,写法太难。

#include <stdio.h>
#include <stdlib.h>
#include <string.h> ///x,y的增量
int dx[] = {,,-,};
int dy[] = {,,,-};
int queue[];
int used[]; ///使用数组piece表示拼图的每一片的信息
struct {
int id; ///这一片,他的相邻的片的编号
int side; ///这一片,他的相邻片的邻边的编号
}piece[][]; ///使用数组pos保存拼图的位置信息,输出结果
struct position{
int x, y; ///坐标
int id; ///卡片的编号
int side; ///下底的编号
}pos[]; int compare(const void *a,const void *b)
{
int c;
if((c=(((struct position*)a)->x-((struct position*)b)->x))!=)
return(c);
return((((struct position*)a)->y-((struct position*)b)->y));
} int main()
{
int n, k; ///卡片的数量和k个卡片的信息量
int i;
int cases = ; while(scanf("%d", &n) && n)
{
scanf("%d", &k);
memset(piece, , sizeof(piece));
memset(used, , sizeof(used));
int a, b, c, d;
for(i=; i<k; i++)
{
scanf("%d%d%d%d", &a, &b, &c, &d);
piece[a][b].id = c;
piece[a][b].side = d;
piece[c][d].id = a;
piece[c][d].side = b;
} ///编号初始化
for(i=; i<n; i++)
pos[i].id = i; pos[].x = ;
pos[].y = ;
pos[].side = ;
used[] = ;
queue[] = ; int first = ; ///队列的头部位置
int count = ; ///队列的尾部位置 ///队列还没有搜索完毕
while (first<=count)
{
///搜索当前节点
a = queue[first]; ///编号
int x0 = pos[a].x;
int y0 = pos[a].y;
d = pos[a].side;
int number; ///相邻卡片的编号 ///对相邻卡片的4边搜索
for(i=; i<; i++)
{
///输入数据有卡片的信息
if((number=piece[a][i].id)>=)
///该卡片还没有搜过
if(used[number]==)
{
b = piece[a][i].side;
used[number] = ; ///计算相邻卡片的位置和底边的信息 ///x&3相当于(x+4)%4
pos[number].x = x0+dx[(i-d)&];
pos[number].y = y0+dy[(i-d)&];
pos[number].side = ((b-i+d+)&); ///该卡片进入队列
queue[++count] = number;
}
}
first++; ///搜索下一个节点
}
qsort(pos, n, sizeof(position), compare);
a = pos[].x;
b = pos[].y;
printf("Instance %d:\n",cases++);
for(i=; i<n; i++)
printf("%5d%5d%6d%2d\n", pos[i].x-a, pos[i].y-b, pos[i].id, pos[i].side);
}
return ;
}

广搜,智能拼图(ZOJ1079)的更多相关文章

  1. HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?

    这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others)  ...

  2. HDU 5652(二分+广搜)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/128683#problem/E 题目大意:给定一只含有0和1的地图,0代表可以走的格子,1代表不能走的格 子.之 ...

  3. nyoj 613 免费馅饼 广搜

    免费馅饼 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy ...

  4. poj 3984:迷宫问题(广搜,入门题)

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7635   Accepted: 4474 Description ...

  5. poj 3278:Catch That Cow(简单一维广搜)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45648   Accepted: 14310 ...

  6. 双向广搜 POJ 3126 Prime Path

      POJ 3126  Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16204   Accepted ...

  7. 广搜+打表 POJ 1426 Find The Multiple

    POJ 1426   Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25734   Ac ...

  8. 双向广搜 codevs 3060 抓住那头奶牛

    codevs 3060 抓住那头奶牛 USACO  时间限制: 1 s  空间限制: 16000 KB  题目等级 : 黄金 Gold   题目描述 Description 农夫约翰被告知一头逃跑奶牛 ...

  9. 双向广搜+hash+康托展开 codevs 1225 八数码难题

    codevs 1225 八数码难题  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond   题目描述 Description Yours和zero在研究A*启 ...

随机推荐

  1. springMVC从前端接受boolean类型的属性失败的问题

    springMVC从前端接收到的实体又一个boolean类型的属性,发现不管前端怎么操作,后台接收到的值都是false. 问题出现原因: eclipse在给实体属性生成getter和setter的时候 ...

  2. 汉诺塔问题hdu 2065——找规律

    这类题目就是纸上模拟,找规律. 问题描述:在一块铜板上有三根杆,目的是将最左边杆上的盘全部移到右边的杆上,条件是不允许直接从最左(右)边移到最右(左)边(每次移动一定是移到中间杆或从中间移出),也不允 ...

  3. Linux数组基础

    执行结果:

  4. js学习笔记 -- Promise

    Promise new Promise( function(resolve, reject) {...} /* executor */  ); executor是带有 resolve 和 reject ...

  5. 3d Max 2018安装失败怎样卸载3dsmax?错误提示某些产品无法安装

    AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...

  6. WSGI学习系列Paste

    Paste has been under development for a while, and has lots of code in it. The code is largely decoup ...

  7. spring boot 配置redis

    先配置属性: # database name spring.redis.database=0 # server host spring.redis.host=127.0.0.1 # server pa ...

  8. 117、python MySQLdb在windows环境下的快速安装、问题解决方式

    使用Python访问MySQL,需要一系列安装 Linux下MySQLdb安装见 Python MySQLdb在Linux下的快速安装 http://blog.csdn.NET/wklken/arti ...

  9. pat1045. Favorite Color Stripe (30)

    1045. Favorite Color Stripe (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  10. pat1025. PAT Ranking (25)

    1025. PAT Ranking (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Programmi ...