广搜,智能拼图(ZOJ1079)
题目链接: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)的更多相关文章
- HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?
这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 5652(二分+广搜)
题目链接:http://acm.hust.edu.cn/vjudge/contest/128683#problem/E 题目大意:给定一只含有0和1的地图,0代表可以走的格子,1代表不能走的格 子.之 ...
- nyoj 613 免费馅饼 广搜
免费馅饼 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy ...
- poj 3984:迷宫问题(广搜,入门题)
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7635 Accepted: 4474 Description ...
- poj 3278:Catch That Cow(简单一维广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45648 Accepted: 14310 ...
- 双向广搜 POJ 3126 Prime Path
POJ 3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16204 Accepted ...
- 广搜+打表 POJ 1426 Find The Multiple
POJ 1426 Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25734 Ac ...
- 双向广搜 codevs 3060 抓住那头奶牛
codevs 3060 抓住那头奶牛 USACO 时间限制: 1 s 空间限制: 16000 KB 题目等级 : 黄金 Gold 题目描述 Description 农夫约翰被告知一头逃跑奶牛 ...
- 双向广搜+hash+康托展开 codevs 1225 八数码难题
codevs 1225 八数码难题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Yours和zero在研究A*启 ...
随机推荐
- 微软原版SQL Helper
代码 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-- ...
- es6数组新方法
(1)Array.from(aarr,fn,obj) function fn(dr, sd, d) { /*Array.from 类数组转化为数组*/ console.log(arguments) v ...
- linux 命令(Ubuntu)
1. 我们可以使用下列的命令压缩一个目录: # zip -r archive_name.zip directory_to_compress 2. 下面是如果解压一个zip文档: # unzip arc ...
- 如何利用fastjson将JSON格式的字符串转换为Map,再返回至前端成为js对象
//注意,这里的jsonStr是json格式的字符串,里面如果遇到双引号嵌套双引号的,一般是嵌套的双引号经过转义 // \",假如有这样的一个场景,这些字符串里面有需要的css样式的j ...
- SpringBoot初始教程之Servlet、Filter、Listener配置详解
1.介绍 通过之前的文章来看,SpringBoot涵盖了很多配置,但是往往一些配置是采用原生的Servlet进行的,但是在SpringBoot中不需要配置web.xml的 因为有可能打包之后是一个ja ...
- linux在命令符界面如何浏览网页
1.介绍 w3m是个开放源代码的命令行下面的网页浏览器. 它支持表格.框架.SSL连线.颜色.如果是在适当的terminal上,甚至还支持"inline image". 这个软件通 ...
- Python APIs
NDArray API Python API速查
- Win7 IIS 局域网中无法访问网页
安装好iis后,在局域网中无法浏览网页一,关闭防火墙即可 或者建立入站规则 打开控制面板——window防火墙——高级设置 在入站规则上右键新建入站规则,选择端口然后下一步 选择tcp和特定端口在端口 ...
- 所有节点配置NTP服务
主节点: 打开vim /etc/ntp.conf文件 For more information about this file, see the man pages # ntp.conf(), ntp ...
- 移动Web开发与适配笔记
项目要是适配手机端,想透彻的把相关内容弄清楚,现在总结一下. 一.移动端开发有如下特点: 1.跑在手机端的web 页面就是h5页面 2.具有跨平台性(web 安卓 iOS都适应) 3.基于webvie ...