题目链接: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. hive中解析json数组

    -- hive中解析json数组 select t1.status ,substr(ss.col,,) as col ,t3.evcId ,t3.evcLicense ,t3.evcAddress , ...

  2. Linux ps命令记录

    ps命令:查看当前系统进程状态 ps -a   显示当前所有进程ps -ax 显示没有控制终端的进程ps -u better   查看用户better的进程ps  aux|less 通过cpu和内存来 ...

  3. mapreduce统计总数

    现有某电商网站用户对商品的收藏数据,记录了用户收藏的商品id以及收藏日期,名为buyer_favorite1. buyer_favorite1包含:买家id,商品id,收藏日期这三个字段,数据以“\t ...

  4. sudo 命令问题详解(一)

    普通用户不能使用sudo命令的解决办法  https://www.cnblogs.com/fasthorse/p/5949946.html 解决sudo: sorry, you must have a ...

  5. 2018.6.1学习CSS5里顺丰盒子小问题

    在制作下面这样的小盒子时 编写的代码 首先要对li元素设置浮动. 在设置这个li元素下面的a元素时, 因为没有转行内块,inline-block,结果显示出来的页面,鼠标在经过这个选择时(:hover ...

  6. input类型为file改变默认按钮样式

    改变 input file 样式(input  文件域)是很多前端朋友经常遇到的头疼问题,今天推荐两种改变 input file 样式的两种常用方法: 方法一: <input type=&quo ...

  7. MATLAB字符串处理

    字符串处理 MATLAB中字符串用单引号   如果字符串中存在单引号,要使用两个单引号. length()元素个数 (1)字符串的执行 格式:eval(s) s表示字符串 abs()和double() ...

  8. Flyweight_pattern--reference

    http://en.wikipedia.org/wiki/Flyweight_pattern In computer programming, flyweight is a software desi ...

  9. mysql java 通用AES加密

    最近有个需求,需要对数据库某些字段加密,调研发现采用AES加密的方式较多,而且反向解密速度快,符合需求,于是采用:下面是遇到的问题及相关代码 首先第一个问题,AES的秘钥是16位,mysql的密码长度 ...

  10. [转]asp.net core视图组件(ViewComponent)简单使用

    本文转自:http://www.cnblogs.com/dralee/p/6170496.html 一.组成: 一个视图组件包括两个部分,派生自ViewComponent的类及其返回结果.类似控制器. ...