カリキュラムデザインプッシュボックス(自動プッシュ可能)
8867 ワード
ブログリンク
自動プッシュボックス部分は二重bfsで実現され、注釈が少し多く、コードが少し乱れています.
自動プッシュボックス部分は二重bfsで実現され、注釈が少し多く、コードが少し乱れています.
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define N 101
using namespace std;
char map[N][N];
char YON;
int show, judge, boom, ph, addb, endgame;
int stax, stay, boxx, boxy, endx, endy, addbx, addby;
int vis[N][N][4], flag[N][N]; // vis
int i, j, step, n, m, showtime, cnt;
int dir[4][2]={-1,0,1,0,0,-1,0,1};
char P[4]= {'W','S','A','D'}; // w,a,s,d
char M[4]= {'w','s','a','d'};
string temp, res;
struct Node
{
int x,y;
int px,py;
string ans;
};
int check(int x,int y) //
{
if(x < 1 || x > n || y < 1 || y > n || map[x][y] == 5)
return 0;
return 1;
}
queueq;
queueque;
/*
BFS, BFS , BFS .
,
*/
int bfs_person(Node a,Node c) //
{
memset(flag,0,sizeof(flag));
Node next,b;
b.x=a.px, b.y=a.py;
b.ans="";
while(!q.empty()) //
q.pop();
q.push(b);
flag[b.x][b.y]=1;
while(!q.empty())
{
Node now=q.front();
q.pop();
if(now.x==a.x&&now.y==a.y)
{
temp=now.ans;
return 1;
}
for(int i=0; i<4; i++) // 4
{
next=now;
next.x+=dir[i][0];
next.y+=dir[i][1];
if(!check(next.x,next.y)||(next.x==c.x&&next.y==c.y)) continue;
if(flag[next.x][next.y]) continue;
flag[next.x][next.y]=1;
next.ans=now.ans+M[i]; // M
q.push(next);
}
}
return 0;
}
string bfs_box() //
{
Node next,pre;
Node st;
memset(vis,0,sizeof(vis));
st.ans = "";
st.px = stax; //
st.py = stay;
st.x = boxx; //
st.y = boxy;
while(!que.empty()) //
que.pop();
que.push(st);
while(!que.empty())
{
Node now = que.front();
que.pop();
if(map[now.x][now.y] == 4) //
return now.ans;
for(i=0; i < 4; i++) // 4
{
next = now;
next.x += dir[i][0];
next.y += dir[i][1];
if(!check(next.x, next.y) || vis[next.x][next.y][i])
continue;
pre=now;
if(i == 0)
pre.x = pre.x + 1;
else if(i == 1)
pre.x = pre.x - 1;
else if(i == 2)
pre.y = pre.y + 1;
else if(i == 3)
pre.y = pre.y - 1;
if(!check(pre.px, pre.py) || !bfs_person(pre, now))
continue;
vis[next.x][next.y][i] = 1;
next.ans = now.ans + temp;
next.ans = next.ans + P[i]; // P
next.px = now.x;
next.py = now.y;
que.push(next);
}
}
return "-1"; // -1
}
void gotoxy(int x,int y) //
{
COORD pos = {x,y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut,pos);
}
void color(int x) //
{
if(x >= 0 && x <= 15)
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),x);
else
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),7);
}
void createmap() //
{
int wall[N][N];
srand(time(NULL));
for(i=0;i> n;
if(n > 100)
cout << " :";
}while(n > 100);
cout << " ( , ,n>=4):";
do
{
cin >> m;
if(m < 4)
cout << " :";
}while(m < 4);
cout << " (n<=5):";
do
{
cin >> addb;
if(addb > 5)
cout << " :";
}while(addb > 5);
cout << " , ";
}
void tips() //
{
gotoxy(n+2,0);
color(3);
cout << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-";
gotoxy(n+2,1);
color(2);
printf("'%c' ( w,s,a,d )",1);
gotoxy(n+2,2);
color(3);
cout << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-";
gotoxy(n+2,3);
color(2);
printf("'%c' ( , )",3);
gotoxy(n+2,4);
color(3);
cout << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-";
gotoxy(n+2,5);
color(2);
printf("'%c' ( )",5);
gotoxy(n+2,6);
color(3);
cout << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-";
gotoxy(n+2,7);
color(2);
printf("'%c' , +2",6);
gotoxy(n+2,8);
color(3);
cout << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-";
gotoxy(n+2,9);
color(2);
printf("'.' ");
gotoxy(n+2,10);
color(3);
cout << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-";
gotoxy(n+2,11);
color(2);
printf("' ' ");
gotoxy(n+2,12);
color(3);
cout << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-";
gotoxy(n+2,13);
color(2);
cout << " h ";
gotoxy(n+2,14);
color(3);
cout << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-";
gotoxy(n+2,15);
color(2);
cout << " p ";
gotoxy(n+2,16);
color(3);
cout << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-";
gotoxy(n+2,17);
color(2);
cout << " b ";
gotoxy(n+2,18);
color(3);
cout << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-";
gotoxy(n+2,19);
color(2);
cout << " q ";
gotoxy(n+2,20);
color(3);
cout << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-";
}
void ismap() //
{
do // do--while ,
{
createmap();
createbox();
addboom();
cnt++;
res = bfs_box();
if(res == "-1")
{
for(i=0;i 0)
{
map[stax-1][stay] = 0;
map[stax+1][stay] = 0;
map[stax][stay-1] = 0;
map[stax][stay+1] = 0;
map[stax-1][stay-1] = 0;
map[stax+1][stay-1] = 0;
map[stax-1][stay+1] = 0;
map[stax+1][stay+1] = 0;
boom--;
}
else
{
gotoxy(n+2,21);
color(4);
cout << " , ......";
getch();
}
}
int main()
{
while(1)
{
system("cls"); //
//
endgame = 0;
ph = 0;
judge = 0;
show = 0;
cnt = 0;
stax = 1;
stay = 1;
showtime = 100;
res = "";
memset(map,0,sizeof(map));
menu();
ismap();
while(1) //
{
system("cls");
color(8);
drawmap(); //
tips();
gotoxy(stay,stax); //
color(5);
printf("%c",1);
gotoxy(boxy,boxx); //
color(4);
printf("%c",3);
//
gotoxy(0,n+1);
color(7);
cout << " :" << step;
gotoxy(0,n+2);
color(7);
cout << " :" << '(' << stax << ',' << stay << ')';
gotoxy(0,n+3);
color(7);
cout << " :" << cnt << endl;
gotoxy(0,n+4);
color(7);
cout << " :";
for(i=0;i> showtime;
automove(res);
//
showtime = 100;
show = 0;
}
gotoxy(0,n+8);
//
if(res == "-1")
{
cout << " , Y/N?";
cin >> YON ;
YON = tolower(YON);
break;
}
if(boxx == endx && boxy == endy)
{
cout << " , Y/N?";
cin >> YON;
YON = tolower(YON);
break;
}
if(endgame == 1)
{
cout << " , Y/N?";
cin >> YON;
YON = tolower(YON);
break;
}
}
if(YON == 'n') //
break;
if(YON == 'y')
continue;
}
}