ural 1008. Image Encoding bfs

来源:互联网 发布:php源代码加密 原理 编辑:程序博客网 时间:2024/06/10 07:47

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1008


题意描述:对一幅黑白格图片(例右图)的描述方式有两种(保证黑格边连通,且横竖坐标介于1~10):

1,第一行输出黑格总数N;接下来N行以  外X升序、内Y升序  的顺序输出每个黑格的坐标,图例即:见原网页

6
2 3
2 4
3 3
3 4
4 2
4 3


2.第一行输出首先最左然后最下的黑格坐标;接下来N行按描述顺序输出每个黑格邻接黑格的描述,从左边开始以逆时针的方式遍历邻接黑格,若该黑格未被描述过则输出相应方向标志(RightTop Left Bottom)每行末尾输出一个英文逗号表示结尾,最后一行的逗号替换成英文点号,图例即:

2 3
RT,
RT,
,
B,
,
.


要求给定任意一种描述方式,输出另外一种描述方式;


思路大致就是相互转换吧,BFS + 二维遍历;


AC代码:

//#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <stdio.h>#include <vector>#include <cstring>#include <queue>using namespace std;enum State{ WHITE, BLACK, VISITED };vector<vector<State> >v(20, vector<State>(20, WHITE));bool printChar(int x, int y, char c){if (v[x][y] == BLACK){printf("%c", c);v[x][y] = VISITED;return true;}return false;}void one(int n){      // 第一种转第二种int x, y, a, b;scanf("%d%d", &x, &y);v[x][y] = BLACK;for (int i = 1; i < n; i++){scanf("%d%d", &a, &b);v[a][b] = BLACK;}printf("%d %d\n", x, y);v[x][y] = VISITED;queue<pair<int, int> >q;q.push(make_pair(x, y));while (!q.empty()){x = q.front().first;y = q.front().second;q.pop();if (printChar(x + 1, y, 'R'))q.push(make_pair(x + 1, y));if (printChar(x, y + 1, 'T'))q.push(make_pair(x, y + 1));if (printChar(x - 1, y, 'L'))q.push(make_pair(x - 1, y));if (printChar(x, y - 1, 'B'))q.push(make_pair(x, y - 1));if (q.empty())printf(".\n");elseprintf(",\n");}}void two(int x, int y){  // 第二种转第一种queue<pair<int, int> > q;q.push(make_pair(x, y));v[x][y] = BLACK;char s[5];int cnt = 0;while (!q.empty()){int a = q.front().first;int b = q.front().second;q.pop(); cnt++;scanf("%s", s);for (size_t i = 0; i < strlen(s) - 1; i++)switch (s[i]){case 'R':v[a + 1][b] = BLACK; q.push(make_pair(a + 1, b)); break;case 'T':v[a][b + 1] = BLACK; q.push(make_pair(a, b + 1)); break;case 'L':v[a - 1][b] = BLACK; q.push(make_pair(a - 1, b)); break;case 'B':v[a][b - 1] = BLACK; q.push(make_pair(a, b - 1)); break;default:break;}}printf("%d\n", cnt);for (size_t i = 0; i < 20; i++)for (size_t j = 0; j < 20; j++)if (v[i][j] == BLACK)printf("%d %d\n", i, j);}void func(){char s[10];gets(s);int a = 0, b = 0;size_t index = 0;while (index != strlen(s)){if (s[index] == ' ')break;a = a * 10 + s[index] - '0';index++;}index++;while (index < strlen(s)){b = b * 10 + s[index] - '0';index++;}if (b)two(a, b);else one(a);}int main(){//freopen("out.txt", "w", stdout);//freopen("in.txt", "r", stdin);func();}


0 0
原创粉丝点击