Решения задач acmp.ru
Главная
Вход
Регистрация
Воскресенье, 27.09.2020, 12:31Приветствую Вас Гость | RSS
Меню сайта

Категории раздела
Pascal [121]
C++ [76]

Поиск

Мини-чат
200

Легкий заработок!

Статистика

Онлайн всего: 1
Гостей: 1
Пользователей: 0

Форма входа

Главная » Файлы » C++

0426 Lines - 2

Скачать файл можно после просмотра рекламы:

05.06.2012, 03:15
#include <stdio.h>
#include <memory.h>

struct uint2
{
unsigned int x, y;
};

int main()
{
unsigned int n = 0;
uint2 target_pos = {0, 0}, start_pos = {0, 0};
char **matrix = NULL, **tmp_matrix = NULL;

FILE* f = fopen("input.txt", "r");
fscanf(f, "%d", &n);

matrix = new char* [n];
tmp_matrix = new char* [n];
for (int i = 0; i<n; i++)
{
matrix [i] = new char [n+3];
tmp_matrix [i] = new char [n];
memset (tmp_matrix [i], 0, sizeof(char) * n);

fscanf(f, "%s", matrix [i]);

for (int j = 0; j<n; j++)
{
switch (matrix [i][j])
{
case 'O':
tmp_matrix [i][j] = 5;
break;

case '@':
start_pos.x = i;
start_pos.y = j;
break;

case 'X':
target_pos.x = i;
target_pos.y = j;
break;
}
}
}

fclose(f);

uint2 *buffer = new uint2 [n * n + n];
unsigned int length = 0, current = 0;
bool work = true;

buffer [0] = start_pos;

do
{
if (buffer [current].x > 0) if (!tmp_matrix [buffer [current].x-1] [buffer [current].y])
{
length++;
buffer [length].x = buffer [current].x - 1;
buffer [length].y = buffer [current].y;
tmp_matrix [buffer [length].x] [buffer [length].y] = 1;
if (buffer [length].x == target_pos.x && buffer [length].y == target_pos.y)
work = false;
}
if (buffer [current].y > 0) if (!tmp_matrix [buffer [current].x] [buffer [current].y-1])
{
length++;
buffer [length].x = buffer [current].x;
buffer [length].y = buffer [current].y - 1;
tmp_matrix [buffer [length].x] [buffer [length].y] = 2;
if (buffer [length].x == target_pos.x && buffer [length].y == target_pos.y)
work = false;
}
if (buffer [current].x < n-1) if (!tmp_matrix [buffer [current].x+1] [buffer [current].y])
{
length++;
buffer [length].x = buffer [current].x + 1;
buffer [length].y = buffer [current].y;
tmp_matrix [buffer [length].x] [buffer [length].y] = 3;
if (buffer [length].x == target_pos.x && buffer [length].y == target_pos.y)
work = false;
}
if (buffer [current].y < n-1) if (!tmp_matrix [buffer [current].x] [buffer [current].y+1])
{
length++;
buffer [length].x = buffer [current].x;
buffer [length].y = buffer [current].y + 1;
tmp_matrix [buffer [length].x] [buffer [length].y] = 4;
if (buffer [length].x == target_pos.x && buffer [length].y == target_pos.y)
work = false;
}

current++;

} while (work && length >= current);

f = fopen("output.txt", "wt");

if (length < current)
fprintf(f, "N");
else
{
unsigned int x = target_pos.x, y = target_pos.y;

do 
{
matrix [x][y] = '+';
switch (tmp_matrix [x][y])
{
case 1:
x++;
break;
case 2:
y++;
break;
case 3:
x--;
break;
case 4:
y--;
break;
}
} while (x != start_pos.x || y != start_pos.y);

fprintf(f, "Y\n");
for (int i = 0; i<n; i++)
fprintf(f, "%s\n", matrix [i]);
}

fclose(f);

return 0;
}
Категория: C++ | Добавил: shum
Просмотров: 898 | Загрузок: 0
Всего комментариев: 0
Добавлять комментарии могут только зарегистрированные пользователи.
[ Регистрация | Вход ]
Это интересно!


Copyright MyCorp © 2020