honor0301 于 2017.12.07 21:31 提问

``````#include <iostream>
#include <fstream>
#include <stack>
#define row 369
#define column 367

using namespace std;

char buf_r[row*column];
char buf_w[row*column];
int mz[row*column]={0};
int i=0,j=0,num=0,m=1,sum=0,n=0,x,y;
std::stack<int> s;

typedef struct tagDIRECTION
{
int x_offset;
int y_offset;
}DIRECTION;
DIRECTION direction_4[] = {{-1,-1}, {-1, 0}, {-1,1}, {0, 1}, {1, 1}, {1,0}, {1, -1}, {0,-1}};

void FindPath(int *mz,stack<int>& s,int i,int m)
{
s.push(i);
mz[i]=m;
while(!s.empty())     //如果栈为空说明该迷宫无解
{
int cur=s.top();
s.pop();
for(i=0;i<8;i++){
x=cur/column+direction_4[i].x_offset;
y=cur%column+direction_4[i].y_offset;
if(x>=0&&y>=0&&x<row&&y<column&&mz[x*column+y]==0&&buf_w[x*column+y]=='0'){
s.push(x*column+y);
mz[x*column+y]=m;   //经过的路置标记,防止走回头路
}
}
}
}

int main(){
ifstream fin;
fin.open("D:\\Study\\project\\SUB1", ios::binary);
fin.close();
ofstream fout("D:\\Study\\project\\SUB2");
for(;i<row*column;i++){
buf_w[i]=buf_r[i];
}
for(i=0;i<row*column;i++){
//给每个阴影块标号
if(buf_w[i]=='0'&&mz[i]==0){
FindPath(mz,s,i,m);
m++;
}
}
for(i=0;i<row*column;i++){
fout << (char)mz[i];
}
fout.close();
cout << "END";
cin.get();
return 0;
}

``````