#include<cstdio>
#include<iostream>
#include<queue>
#include<ctime>
#include<algorithm>
#include<string>
#include "windows.h"
using namespace std;
int dx[12] = {0,0,-1,1,1,1,-1,-1,2,2,0,0};
int dy[12] = {1,-1,0,0,-1,-1,1,1,0,0,2,2};
int n=0,m=0,eye=1,exp=200,door=0;
char mig[173][173];
bool vis[173][173];
struct node{
	int x,y,d;
};
int dis[31][31];
void all_look(int kx,int ky){
	for(int i=0;i<=n+1;i++){
		for(int j=0;j<=n+1;j++){
			if(i==kx && j==ky) {
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);//设置红色
				printf("@");
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY);
			}
			else printf("%c",mig[i][j]); 
		}
		printf("\n");
	}
}
bool check(int xxx,int yyy){
	if(xxx > n || yyy > n || xxx < 1 || yyy < 1) return false;
	if(mig[xxx][yyy] == '*') return false;
	if(dis[xxx][yyy] != -1) return false;
	return true;
}
void build_check(int x,int y){
	if(mig[x][y]=='*') return;
	if(mig[x][y]=='A') return;
	if(mig[x][y]=='0') return;
	if(mig[x][y]=='X') return;
	mig[x][y]='0';
	for(int i=0;i<4;i++){
		build_check(x+dx[i],y+dy[i]);
	}
}
int waymin(int x,int y,int ex,int ey){//这里采用广度优先搜索,否则会超时。 
	int mw=2;
	for(int i=0;i<4;i++){
		if(mig[x+dx[i]][y+dy[i]]=='0') mig[x+dx[i]][y+dy[i]]='+';
	}
	while(mw<5000){
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				if(mig[i][j]=='+'){
					for(int k=0;k<4;k++){
						if(mig[i+dx[k]][j+dy[k]]=='0') mig[i+dx[k]][j+dy[k]]='#';
						else if(mig[i+dx[k]][j+dy[k]]=='X') return mw;
					}
				}
			}
		}
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				if(mig[i][j]=='#') mig[i][j]='+';
			}
		}
		mw++;
	}
	return -100000;
}
int show(int oo,int pp){
	mig[oo][pp] = 'O';
	for(int i = 1;i <= n;i++){
		for(int j = 1;j <= n;j++)
			printf("%c",mig[i][j]);
		printf("\n");
	}
	return 0;
}


void look(int sx,int sy){
	if(mig[sx - 1][sy] == '*' || mig[sx - 1][sy] == 'A') printf("  ?\n %c%c%c\n",mig[sx - 1][sy - 1],mig[sx - 1][sy],mig[sx - 1][sy + 1]);
	else printf("  %c\n %c%c%c\n",mig[sx - 2][sy],mig[sx - 1][sy - 1],mig[sx - 1][sy],mig[sx - 1][sy + 1]);
	if(mig[sx][sy - 1] == '*' || mig[sx][sy - 1] == 'A') printf("?%cV%c",mig[sx][sy - 1],mig[sx][sy + 1]);
	else printf("%c%cV%c",mig[sx][sy - 2],mig[sx][sy - 1],mig[sx][sy + 1]);
	if(mig[sx][sy + 1] == '*' || mig[sx][sy + 1] == 'A') printf("?\n");
	else printf("%c\n",mig[sx][sy + 2]);
	if(mig[sx + 1][sy] == '*' || mig[sx + 1][sy] == 'A') printf(" %c%c%c\n  ?\n",mig[sx + 1][sy - 1],mig[sx + 1][sy],mig[sx + 1][sy + 1]);
	else printf(" %c%c%c\n  %c\n",mig[sx + 1][sy - 1],mig[sx + 1][sy],mig[sx + 1][sy + 1],mig[sx + 2][sy]);
}
int main(){
//	开局  
    int mod=0;
	srand(time(NULL));
	if(n==0){
		printf("欢迎来到迷宫游戏!\n你的视野长度为2格。\n请问你要多大的地图(最大70)?[输入0进入闯关模式]\n");
		scanf("%d",&n);
	}	
	if(n==0) n=71,m=2500;
	if(n==71){
		if(exp<door*300){
			printf("你输了,成绩:%d 最终分数:%d",door,exp);
			return 0;
		}
		else printf("第%d关,你的分数:%d,本关分数线:%d\n",door+1,exp,(door+1)*300);
		door++;
	}
	else {
		printf("请问你要放几个障碍?\n");
		scanf("%d",&m);
	}	
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY);
	for(int i = 0;i <= n + 1;i++) mig[0][i] = mig[n + 1][i] = mig[i][0] = mig[i][n + 1] = 'A';
//	设置障碍 
	int ax=1,ay=1,aax=1,aay=1,xxx; 
	mig[ax][ay]='*';
	mig[aax][aay]='-';
	do{
		for(int i = 1;i <= n;i++)
			for(int j = 1;j <= n;j++)
				mig[i][j] = '-';
		int a = rand() % n + 1;
		int b = rand() % n + 1;
		mig[a][b] = '*';
		for(int i = 2;i <= m;i++){
			int w = rand() % 11;
			if(w < 8){
				a += dx[w];
				b += dy[w];
				if(a > n || a < 1 || b > n || b < 1){
					a -= dx[i];
					b -= dy[i];
					i--;
					continue;
				}
			}
			else{
				do{
					a = rand() % n + 1;
					b = rand() % n + 1;
				}while(mig[a][b] != '-');
			}
			mig[a][b] = '*';
		}
		do{
			ax = rand() % n + 1;
			ay = rand() % n + 1;
		}while(mig[ax][ay] != '-');
		mig[ax][ay] = '@';
		build_check(ax,ay);
		do{
			aax = rand() % n + 1;
			aay = rand() % n + 1;
		}while(mig[aax][aay] != '0');
		//printf("%d %d %d %d\n",ax,ay,aax,aay);
	}while(max(ax-aax,aax-ax)+max(ay-aay,aay-ay)<n/2);
	mig[aax][aay] = 'X';
	xxx = waymin(ax,ay,aax,aay);
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(mig[i][j]=='#' || mig[i][j]=='0') mig[i][j]='+';
		}
	}
//	正式开始  
	printf("开始游戏!\n");	
	int cnt = 0;
	while(ax != aax || ay != aay){
		printf("你在(%d,%d)\n终点在(%d,%d)\n",ax,ay,aax,aay);
		if(eye==0)all_look(ax,ay);
		else if(eye==1)look(ax,ay);
		printf("M:%d\n",mod);
		printf("w:上,s:下,a:左,d:右,e:退出。r:切换视角。sys:清屏。\n");
		string d;
		cin >> d;
		mig[ax][ay] = '+';
		char c = d[0];
		cnt++;
		if(c == 'e') {
			printf("ll maze1[101][101]={{0");
			for(int i=1;i<=100;i++){
				printf(",0");
			} 
			printf("},\n");
			for(int i=1;i<=100;i++){
				printf("{0");
				for(int j=1;j<=100;j++){
					printf(",  ");
					if(mig[i][j]=='*') printf("1");
					else printf("2"); 
				} 
				if(i!=100)printf("},\n");
				else printf("}};;");
			}
			
			
			
			
		}
		else if(c == 'r') {
			eye=(eye+1)%2;	
			cnt--;
		}
		else if(d == "sys"){
			system("cls");
		}
		else if(d=="check") {
			for(int i=1;i<=n;i++){
				for(int j=1;j<=n;j++){
					if(mig[i][j]=='+') mig[i][j]='-';
				}
			}
			build_check(ax,ay);
			for(int i=1;i<=n;i++){
				for(int j=1;j<=n;j++){
					if(mig[i][j]=='0') mig[i][j]='+';
				}
			}
		}
		else if(d=="go"){
			for(int i=1;i<=n;i++){
				for(int j=1;j<=n;j++){
					if(mig[i][j]=='*'){
						int b_t=0,b_f=0;
						for(int k=0;k<4;k++){
							if(mig[i+dx[k]][j+dy[k]]=='-') b_f=1;
							if(mig[i+dx[k]][j+dy[k]]=='+') b_t=1;
						}
						if(b_t*b_f!=0) {
							mig[i][j]='+';
							build_check(ax,ay);
							for(int i=1;i<=n;i++){
								for(int j=1;j<=n;j++){
									if(mig[i][j]=='0') mig[i][j]='+';
								}
							}
						}
					}
				}
			}
		}
	    else if(d=="mod"){
	    	mod=(mod+1)%2; 
		}
		else if(d=="aa"){
			for(int i=1;i<=100;i++){
				mig[i][1]='*';
			}
		}
		else if(d=="ww"){
			for(int i=1;i<=100;i++){
				mig[1][i]='*';
			}
		}
		else if(d=="ss"){
			for(int i=1;i<=100;i++){
				mig[100][i]='*';
			}
		}
		else if(d=="dd"){
			for(int i=1;i<=100;i++){
				mig[i][100]='*';
			}
		}
		else if(c=='p') {
			for(int k=0;k<4;k++){
 			    if(mig[ax+dx[k]][ay+dy[k]]!='A') mig[ax+dx[k]][ay+dy[k]]='+';
			}
		}
		else if(d=="bool_check"){
			int b_c=0,b_cx,b_cy;
			for(int i=1;i<=n;i++){
				for(int j=1;j<=n;j++){
					if(mig[i][j]=='-'){
						b_c=1;
						b_cx=i,b_cy=j;
					} 
				}
			}
			printf("%d\n",b_c);
			if(b_c==1){
				ax=b_cx,ay=b_cy;
			}
		}
		else if(c == 'w' && ax > 1 && (mig[ax - 1][ay] != '*'||mod==1) && mig[ax - 1][ay] != 'A') {
			if(mod==1) while(mig[ax-1][ay]!='*') ax--;
			else ax--;
		}
		else if(c == 'a' && ay > 1 && (mig[ax][ay - 1] != '*'||mod==1) && mig[ax][ay - 1] != 'A') {
			if(mod==1) while(mig[ax][ay-1]!='*') ay--;
			else ay--;
		}
		else if(c == 's' && ax < n && (mig[ax + 1][ay] != '*'||mod==1) && mig[ax + 1][ay] != 'A'){
			if(mod==1) while(mig[ax+1][ay]!='*') ax++;
			else ax++;
		} 
		else if(c == 'd' && ay < n && (mig[ax][ay + 1] != '*'||mod==1) && mig[ax][ay + 1] != 'A'){
			if(mod==1) while(mig[ax][ay+1]!='*') ay++;
			else ay++;
		} 
		else if(d=="--"){
			for(int i=1;i<=n;i++){
				for(int j=1;j<=n;j++){
					if(mig[i][j]=='+') mig[i][j]='-';
				}
			}
		}
		else{
			printf("无效操作!");
			cnt--;
			mig[ax][ay] = 'O';
			continue;
		}
		mig[ax][ay] = 'O';
	}
	printf("你用了%d步\n最少%d步\n",cnt,xxx);
	all_look(ax,ay); 
	if(n==71){
		printf("\n\n\n----------------------------------------------------------------------------\n你获得了%d分\n输入任意继续。\n\n\n",500-cnt+xxx); 
		exp+=500-cnt+xxx;
		string ffffffff;
		cin >> ffffffff;
		system("cls");
		main();
	}
	return 0;
}