//
// main.cpp
// PlayGame
//
// Created by Apple on 2019/9/7.
// Copyright © 2019 Apple_Lance. All rights reserved.
//
#include <iostream>
#include <stdio.h>
#include <memory.h>
#define MAX 75
char board[MAX + 2][MAX + 2];
int minstep, w, h, to[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
bool mark[MAX + 2][MAX + 2];
void search(int now_x, int now_y, int end_x, int end_y, int step, int f){
if(step > minstep)
return;
if(now_x == end_x && now_y == end_y){
if(minstep > step){
minstep = step;
return;
}
}
for(int i = 0;i < 4;i ++){
int x = now_x + to[i][0];
int y = now_y + to[i][1];
if((x > -1) && (x < w + 2) && (y > -1) && (y < h + 2) && (((board[y][x] == ' ') && (mark[y][x] == false)) || ((x == end_x) && (y == end_y) && (board[y][x] == 'X')))){
mark[y][x] = true;
if(f == i)
search(x, y, end_x, end_y, step, i);
else
search(x, y, end_x, end_y, step + 1, i);
mark[y][x] = false;
}
}
}
int main(int argc, const char * argv[]) {
int Boardnum = 0;
while(scanf("%d%d", &w, &h) != EOF){
if(w == 0 && h == 0)
break;
Boardnum++;
printf("Boardnum #%d:\n", Boardnum);
for(int i = 0; i < MAX + 2;i++)
board[0][i] = board[i][0] = ' ';
for(int i = 1;i <= h;i++){
getchar();
for(int j = 1;j <= w;j++)
board[i][j] = getchar();
}
for(int j = 1;j <= w + 1;j++)
board[h + 1][j] = ' ';
for(int i = 1;i <= h + 1;i++)
board[i][w + 1] = ' ';
int begin_x, begin_y, end_x, end_y, count = 0;
while(scanf("%d %d %d %d", &begin_x, &begin_y, &end_x, &end_y) && begin_x > 0){
count++;
minstep = 100000;
memset(mark, false, sizeof(mark));
search(begin_x, begin_y, end_x, end_y, 0, -1);
if(minstep < 100000)
printf("Pair %d: %d segments.\n", count, minstep);
else
printf("Pair %d: Impossible. \n", count);
}
printf("\n");
}
return 0;
}
第二十九天:小游戏-递归应用
猜你喜欢
转载自blog.csdn.net/lancecrazy/article/details/100595550
今日推荐
周排行