题目链接: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. AD域账号验证

    public partial class _Default : Page { [DllImport("advapi32.dll")] private static extern b ...

  2. linux查找重复文件

    >/dev/ >/dev/null|grep 02a42c7a845094a8904f7b3faf686b81 uniq -d, --repeated only print duplica ...

  3. centos7-根据端口号查看所属应用

    [root@cent7-zuoys ~]# netstat -lnp | grep 8080 [root@cent7-zuoys ~]# ps 4735

  4. spring中注解注入 context:component-scan 的使用说明

    通常情况下我们在创建spring项目的时候在xml配置文件中都会配置这个标签,配置完这个标签后,spring就会去自动扫描base-package对应的路径或者该路径的子包下面的java文件,如果扫描 ...

  5. 还是畅通工程(prim和kruskal)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1233 还是畅通工程 Time Limit: 4000/2000 MS (Java/Others)    ...

  6. PlayMaker Action的执行顺序

    如图:默认的是从上到下 先执行Play Sound,再执行Destroy Object. 可以点击右上角的齿轮,也就是设置按钮选中Action Sequence,这样就会同时执行.

  7. [转]创建一个JavaScript弹出DIV窗口层的效果

    本文转自:http://www.soso.io/article/23698.html <!doctype html> <html lang="en"> &l ...

  8. [转]使用jquery dataTable

    本文转自:http://blog.csdn.net/llhwin2010/article/details/8663753 jQuery 的插件 dataTables 是一个优秀的表格插件,提供了针对表 ...

  9. SimpleAdapter与listview,gridview的组合用法

    首先要明白SimpleAdapter构造方法的几个参数的含义: public SimpleAdapter(Context context, List<? extends Map<Strin ...

  10. http学习笔记(三):报文

                             三.报文 目录: 3.1方法 1.get 2.head 3.put 4.post 5.trace 6.options 7.delete 3.2状态码 ...