博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj3194
阅读量:7236 次
发布时间:2019-06-29

本文共 1607 字,大约阅读时间需要 5 分钟。

简单题, BFS

ContractedBlock.gif
ExpandedBlockStart.gif
View Code
#include 
#include
#include
#include
using namespace std; #define maxn 105 struct Point {
int x, y; Point() {
} Point(int xx, int yy): x(xx), y(yy) {
} }q[maxn * maxn]; int n; int map[maxn][maxn]; bool vis[maxn][maxn]; int dir[4][2] = {
{
1, 0},{
0, 1},{-1, 0},{
0, -1}}; void input() {
memset(map, 0, sizeof(map)); for (int i = 1; i < n; i++) for (int j = 0; j < n; j++) {
int a, b; scanf("%d%d", &a, &b); a--; b--; map[a][b] = i; } } int bfs(Point s) {
int front, rear; front = rear = 0; q[rear++] = s; vis[s.x][s.y] = true; int ret = 1; while (front != rear) {
Point p = q[front++]; for (int i = 0; i < 4; i++) {
Point a(p.x + dir[i][0], p.y + dir[i][1]); if (a.x >= 0 && a.y >= 0 && a.x < n && a.y < n && !vis[a.x][a.y] && map[a.x][a.y] == map[p.x][p.y]) {
vis[a.x][a.y] = true; q[rear++] = a; ret++; } } } return ret; } bool work() {
memset(vis, 0, sizeof(vis)); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (!vis[i][j]) {
int x = bfs(Point(i, j)); if (x != n) return false; } return true; } int main() {
//freopen("t.txt", "r", stdin); while (scanf("%d", &n), n) {
input(); if (work()) printf("good\n"); else printf("wrong\n"); } return 0; }

转载地址:http://lpofm.baihongyu.com/

你可能感兴趣的文章
VUE的总结(1)
查看>>
【PWA学习与实践】(5)在Web中进行服务端消息推送
查看>>
WebAssembly完全入门——了解wasm的前世今身
查看>>
SAP CRM和C4C数据同步的两种方式概述:SAP PI和HCI
查看>>
SAP Cloud for Customer Extensibility的设计与实现
查看>>
Nacos 发布0.3.0版本,迄今为止最好看的版本
查看>>
如何愉快的在PhpStorm中进行PHPUnit单元测试和Xdebug断点调试?
查看>>
Pod Preset玩转K8S容器时区自动配置
查看>>
PHP多进程初探 --- 进程间通信二三事
查看>>
[源码阅读]解析Anime(JS动画库)核心(1)
查看>>
深入http协议原理
查看>>
服务器运维基础指南
查看>>
Vue 全站缓存之 keep-alive : 动态移除缓存
查看>>
记一次基于vue的spa多页签实践经验
查看>>
Android中的设计模式之状态模式
查看>>
打包工具的配置教程见的多了,但它们的运行原理你知道吗?
查看>>
【docker】小技巧:在宿主机器上直接查看docker容器的进程
查看>>
流畅的python读书笔记-第八章-对象引用、可变性和垃圾回收
查看>>
【跃迁之路】【457天】刻意练习系列216(2018.05.08)
查看>>
CSS 水平垂直居中
查看>>