推箱子

1、

#include<stdio.h>
#include<stdlib.h>
#include<getch.h>

int main(int argc,const char* argv[])
{
char map[8][8] = { {0,0,3,3,3,3,0,0},
{0,0,3,5,5,3,0,0},
{0,3,3,0,5,3,3,0},
{0,3,0,0,4,5,3,0},
{3,3,0,4,0,0,3,3},
{3,0,0,3,4,4,0,3},
{3,0,0,2,0,0,0,3},
{3,3,3,3,3,3,3,3}};
int cnt=0,step=0;
int human_x=6,human_y=3;
for( ; ; )
{
system(“clear”);
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
if(0 == map[i][j])
{
printf("%c “,’ ');
}
else if(2 == map[i][j] || 7 == map[i][j])
{
printf(”%c “,’@’);
}
else if(3 == map[i][j])
{
printf(”%c “,’#’);
}
else if(4 == map[i][j])
{
printf(”%c “,’$’);
}
else if(5 == map[i][j])
{
printf(”%c “,‘o’);
}
else if(9 == map[i][j])
{
printf(”%c ",’&’);
}

}
printf("\n");
if(9 == map[1][3] && 9 == map[1][4] && 9 == map[2][4] && 9 == map[3][5])
{
printf(“任务完成”);
printf("%d",step);
return 0;
}
}
switch(getch())
{
case 183:
if( 3 != map[human_x-1][human_y])
{
if(0 == map[human_x-1][human_y])
{
map[human_x][human_y] -= 2;
map[–human_x][human_y] += 2;
}
else if(9 == map[human_x-1][human_y] && 9!= map[human_x-2][human_y] && 4!= map[human_x-2][human_y] && 3!= map[human_x-2][human_y])
{
map[human_x][human_y] -= 2;
map[–human_x][human_y] += 2;
map[human_x][human_y] -= 4;
map[–human_x][human_y] += 4;
human_x++;
}
else if(5 == map[human_x-1][human_y])
{
map[human_x][human_y] -= 2;
map[–human_x][human_y] += 2;
}
else if(4 == map[human_x-1][human_y] && 4!=map[human_x-2][human_y] && 3!=map[human_x-2][human_y] )
{
map[human_x][human_y] -= 2;
map[–human_x][human_y] += 2;
map[human_x][human_y] -= 4;
map[–human_x][human_y] += 4;
human_x++;
}
}
step++;break;
case 184:
if(3 != map[human_x+1][human_y])
{
if(0 == map[human_x+1][human_y])
{
map[human_x][human_y] -= 2;
map[++human_x][human_y] += 2;
}
else if(9 == map[human_x+1][human_y] && 9!= map[human_x+2][human_y] && 4!= map[human_x+2][human_y] && 3!= map[human_x+2][human_y])
{
map[human_x][human_y] -= 2;
map[++human_x][human_y] += 2;
map[human_x][human_y] -= 4;
map[++human_x][human_y] += 4;
human_x–;
}
else if(5 == map[human_x+1][human_y])
{
map[human_x][human_y] -= 2;
map[++human_x][human_y] += 2;
}
else if(4 == map[human_x+1][human_y] &&4!=map[human_x+2][human_y] && 3!=map[human_x+2][human_y] )
{
map[human_x][human_y] -= 2;
map[++human_x][human_y] += 2;
map[human_x][human_y] -= 4;
map[++human_x][human_y] += 4;
human_x–;
}
}
step++;break;
case 185:
if( 3 != map[human_x][human_y+1])
{
if(0 == map[human_x][human_y+1])
{
map[human_x][human_y] -= 2;
map[human_x][++human_y] += 2;
}
else if(9 == map[human_x][human_y+1] && 9 != map[human_x][human_y+2] && 4!= map[human_x][human_y+2] && 3!= map[human_x][human_y+2])
{
map[human_x][human_y] -= 2;
map[human_x][++human_y] += 2;
map[human_x][human_y] -= 4;
map[human_x][++human_y] += 4;
human_y–;
}
else if(5 == map[human_x][human_y+1])
{
map[human_x][human_y] -= 2;
map[human_x][++human_y] += 2;
}
else if(4 == map[human_x][human_y+1] &&4!=map[human_x][human_y+2] && 3!=map[human_x][human_y+2] ) {
map[human_x][human_y] -= 2;
map[human_x][++human_y] += 2;
map[human_x][human_y] -= 4;
map[human_x][++human_y] += 4;
human_y–;
}
}
step++;break;
case 186:
if( 3 != map[human_x][human_y-1])
{
if(0 == map[human_x][human_y-1])
{
map[human_x][human_y] -= 2;
map[human_x][–human_y] += 2;
}
else if(9 == map[human_x][human_y-1] && 9 != map[human_x][human_y-2] && 4!= map[human_x][human_y-2] && 3!= map[human_x][human_y-2])
{
map[human_x][human_y] -= 2;
map[human_x][–human_y] += 2;
map[human_x][human_y] -= 4;
map[human_x][–human_y] += 4;
human_y++;
}
else if(5 == map[human_x][human_y-1])
{
map[human_x][human_y] -= 2;
map[human_x][–human_y] += 2;
}
else if(4==map[human_x][human_y-1] &&4!=map[human_x][human_y-2] && 3!=map[human_x][human_y-2] )
{
map[human_x][human_y] -= 2;
map[human_x][–human_y] += 2;
map[human_x][human_y] -= 4;
map[human_x][–human_y] += 4;
human_y++;
}
}
step++;break;
}
}
}

2、

#include <stdio.h>
#include <stdlib.h>
#include <getch.h>

int main()
{
char map[8][8] = {
{0,0,3,3,3,3,0,0},
{0,0,3,5,5,3,0,0},
{0,3,3,0,5,3,3,0},
{0,3,0,0,4,5,3,0},
{3,3,0,4,0,0,3,3},
{3,0,0,3,4,4,0,3},
{3,0,0,2,0,0,0,3},
{3,3,3,3,3,3,3,3},
};//数组

int x = 6 , y = 3 , step = 0;

for(; ; )
{
system(“clear”);

int cnt = 0;
for(int i=0; i<8; i++)
{
for(int j=0; j<8; j++)
{
switch(map[i][j])
{
case 0: printf(" “); break;
case 2: printf(”@ “); break;
case 3: printf(”# “); break;
case 4: printf(”$ "); break;
case 5: printf(“O “); break;
case 7: printf(”@ “); break;
case 9: printf(”$ “); cnt++;
}//输出地图
}
printf(”\n”);
}

if(4 == cnt)
{
printf(“恭喜完成任务,一共走%d步!\n”, step);
return 0;
}

int ox = 0 , oy = 0;
switch(getch())
{
case 183: ox–; break;//上
case 184: ox++; break;//下
case 185: oy++; break;//右
case 186: oy–; break;//左

}

if(0 == map[x+ox][y+oy] || 5 == map[x+ox][y+oy])
{
map[x+ox][y+oy] += 2;
map[x][y] -= 2;
x += ox;
y += oy;
step++;//记录步数
continue;
}

if((4 == map[x+ox][y+oy] || 9==map[x+ox][y+oy]) &&
(0 == map[x+ox2][y+oy2] || 5 == map[x+ox2][y+oy2]))
{
map[x+ox2][y+oy2] += 4;
map[x+ox][y+oy] -= 2;
map[x][y] -= 2;
x += ox;
y += oy;
step++;
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值