URAL 1008 Image Encoding (BFS)
#include <stdio.h>
#define SIZE 10
int n;
int leftBottomX, leftBottomY;
int x, y;
int image[SIZE + 1][SIZE + 1];
typedef struct{
int x;
int y;
} PIXEL;
PIXEL queue[SIZE * SIZE + 1];
int head, tail;
PIXEL pixelPoped, pixelPushed;
int pixelVisited[SIZE + 1][SIZE + 1];
int directions[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};
void BFS1( ){
int leftBottomPixelFound = 0;
int indexOfBlack;
for (indexOfBlack = 1; indexOfBlack <= n; indexOfBlack++){
scanf("%d %d", &x, &y);
image[x][y] = 1;
if (!leftBottomPixelFound){
leftBottomPixelFound = 1;
leftBottomX = x;
leftBottomY = y;
}
}
printf("%d %d
", leftBottomX, leftBottomY);
pixelPushed.x = leftBottomX;
pixelPushed.y = leftBottomY;
pixelVisited[leftBottomX][leftBottomY] = 1;
queue[tail++] = pixelPushed;
while (head < tail){
pixelPoped = queue[head++];
int indexOfDirection;
for (indexOfDirection = 0; indexOfDirection < 4; indexOfDirection++){
x = pixelPoped.x + directions[indexOfDirection][0];
y = pixelPoped.y + directions[indexOfDirection][1];
if (image[x][y] == 0 ||
x < 1 || x > SIZE ||
y < 1 || y > SIZE ||
pixelVisited[x][y] == 1)
continue;
pixelVisited[x][y] = 1;
char charPrinted;
if (indexOfDirection == 0)
charPrinted = 'R';
else if (indexOfDirection == 1)
charPrinted = 'T';
else if (indexOfDirection == 2)
charPrinted = 'L';
else if (indexOfDirection == 3)
charPrinted = 'B';
printf("%c", charPrinted);
pixelVisited[x][y] = 1;
pixelPushed.x = x;
pixelPushed.y = y;
queue[tail++] = pixelPushed;
}
printf("%c
", head == tail ? '.' : ',');
}
}
void BFS2(){
leftBottomX = n;
scanf("%d", &leftBottomY);
image[leftBottomX][leftBottomY] = 1;
pixelPushed.x = leftBottomX;
pixelPushed.y = leftBottomY;
queue[tail++] = pixelPushed;
while (head < tail){
pixelPoped = queue[head++];
char directionStr[6];
scanf("%s", directionStr);
int indexOfChar;
for (indexOfChar = 0; directionStr[indexOfChar] != '\0'; indexOfChar++){
if (directionStr[indexOfChar] == '.' || directionStr[indexOfChar] == ',')
break;
int indexOfDiretion;
if (directionStr[indexOfChar] == 'R')
indexOfDiretion = 0;
else if (directionStr[indexOfChar] == 'T')
indexOfDiretion = 1;
else if (directionStr[indexOfChar] == 'L')
indexOfDiretion = 2;
else if (directionStr[indexOfChar] == 'B')
indexOfDiretion = 3;
x = pixelPoped.x + directions[indexOfDiretion][0];
y = pixelPoped.y + directions[indexOfDiretion][1];
if (image[x][y] == 1)
continue;
image[x][y] = 1;
pixelPushed.x = x;
pixelPushed.y = y;
queue[tail++] = pixelPushed;
}
}
int numOfBlacks = head;
printf("%d
", numOfBlacks);
for (x = 1; x <= SIZE; x++)
for (y = 1; y <= SIZE; y++)
if (image[x][y] == 1)
printf("%d %d
", x, y);
}
int main()
{
scanf("%d", &n);
char cha = getchar();
if (cha == '
')
BFS1();
else
BFS2();
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Java/PDF에서 이미지 바꾸기우리 모두 알다시피 PDF는 편집하기 어려운 일종의 문서 형식입니다. 그러나 다른 사람으로부터 PDF 문서를 받을 때 문서의 이미지를 새 이미지로 바꾸는 등 약간의 수정이 필요할 수 있습니다. 이 문서에서는 Java...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.