chapter 2 section 2.4 Overfencing

来源:互联网 发布:mac更改磁盘格式 编辑:程序博客网 时间:2024/05/16 14:53

 /*
ID: niepeng1
LANG: C++
TASK: maze1
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <iostream>
using namespace std;

struct Node{
 int i;
 int j;
 int num;
};
char map[202][80];
int map2[201][80];
int w,h,w2,h2,max2=0;
int point[2][2];

void Dfs(int i,int j,int count)
{
 if( map[i][j-1] != '|' && (map[i][j-2] == ' ' || map2[i][j-2] > count+1) ){
  map[i][j-2] = 'a';
  map2[i][j-2] = count+1;
  Dfs(i,j-2,count+1);
 }
 if(map[i][j+1] != '|' && (map[i][j+2] == ' ' || map2[i][j+2] > count+1) ){
   map[i][j+2] = 'a';
   map2[i][j+2] = count+1;
   Dfs(i,j+2,count+1);
 }
 if(map[i-1][j] != '-' && (map[i-2][j] == ' ' || map2[i-2][j] > count+1) ){
   map[i-2][j] = 'a';
   map2[i-2][j] = count+1;
   Dfs(i-2,j,count+1);
 }
 if(map[i+1][j] != '-' && (map[i+2][j] == ' ' || map2[i+2][j] > count+1) ){
   map[i+2][j] = 'a';
   map2[i+2][j] = count+1;
   Dfs(i+2,j,count+1);
 }
}
int main(void)
{
 int i,j;
 char c;
 FILE *in, *out;
 in = fopen("maze1.in", "r");
 out = fopen("maze1.out", "w+");

 fscanf(in,"%d",&w); w2 = 2*w;
 fscanf(in,"%d",&h); h2 = 2*h;
 fscanf(in,"%c",&c);
 for(i=0; i<=h2; i++)
 {
  for(j=0; j<=w2; j++)
  {
   map2[i][j]=0;
   fscanf(in,"%c",&map[i][j]);
  }
  fscanf(in,"%c",&c);
 }
 for(j=0,i=0;i<=h2;i++){
  if(map[i][0] == ' '){
   point[j][0] = i;
   point[j][1] = 1;
   map[i][0] = '|';
   j++;
  }
  if(map[i][w2] == ' '){
   point[j][0] = i;
   point[j][1] = w2-1;
   map[i][w2] = '|';
   j++;
  }
 }
 for(i=0;i<=w2;i++){
  if( map[0][i] == ' '){
   point[j][0] = 1;
   point[j][1] = i;
   map[0][i] = '-';
   j++;
  }
  if(map[h2][i] == ' '){
   point[j][0] = h2-1;
   point[j][1] = i;
   map[h2][i] = '-';
   j++;
  }
 }
 map2[ point[0][0] ][ point[0][1] ] =1;
 map2[ point[1][0] ][ point[1][1] ] =1;
 Dfs(point[0][0],point[0][1],1);
 Dfs(point[1][0],point[1][1],1);

 for(i=1;i<=h2-1;i+=2)
  for(j=1;j<=w2-1;j+=2)
  {
   if( max2 < map2[i][j])
    max2=map2[i][j];
  }
 fprintf(out,"%d/n",max2);//out,
 //printf("%d/n",max2);
 fclose(in);
 fclose(out);
 
 return 0;
}

原创粉丝点击