Discuz! Board

 找回密码
 立即注册
查看: 338|回复: 1

迷宫2023.2.26

[复制链接]

20

主题

33

帖子

3458

积分

论坛元老

Rank: 8Rank: 8

积分
3458

小火马 Lv:11
发表于 2023-2-26 10:57:58 | 显示全部楼层 |阅读模式
[C++] 纯文本查看 复制代码
#include <bits/stdc++.h>
using namespace std;
int main() {
	char map[3][4] = {
		'A', '0', '0', '0',
		'0', '#', '#', '0',
		'0', '#', '0', '0'
	};
	char a[100];
	cin >> a;
	int x = 0, y = 0;
	for (int i = 0; i <= 2; i++) {
		for (int j = 0; j <= 3; j++) {
			cout << map[i][j] << " ";
		}
		cout << endl;
	}
	cout << endl;
	map[0][0] = '0';
	for (int i = 0; i < strlen(a); i++) {
		switch (a[i]) {
			case 'w':
				if (x != 0 && map[x - 1][y] != '#') {
					x = x - 1;
				}
				break;
			case 'a':
				if (y != 0 && map[x][y - 1] != '#') {
					y = y - 1;
				}
				break;
			case 's':
				if (x != 2 && map[x + 1][y] != '#') {
					x = x + 1;
				}
				break;
			case 'd':
				if (y != 3 && map[x][y + 1] != '#') {
					y = y + 1;
				}
				break;
		}
	}
	map[x][y] = 'A';
	for (int i = 0; i <= 2; i++) {
		for (int j = 0; j <= 3; j++) {
			cout << map[i][j] << " ";
		}
		cout << endl;
	}
	return 0;
}
回复

使用道具 举报

20

主题

33

帖子

3458

积分

论坛元老

Rank: 8Rank: 8

积分
3458

小火马 Lv:11
 楼主| 发表于 2023-2-26 18:08:14 | 显示全部楼层
[C++] 纯文本查看 复制代码
#include <bits/stdc++.h>
using namespace std;
int main() 
{
    char map[3][4] = 
	{
        'A', '0', '0', '0',
        '0', '#', '#', '0',
        '0', '#', '0', '0'
    };
    char a[100];
    cin >> a;
    int x = 0, y = 0;
    for (int i = 0; i <= 2; i++) 
	{
        for (int j = 0; j <= 3; j++) 
		{
            cout << map[i][j] << " ";
        }
        cout << endl;
    }
    map[0][0]='1';
    cout << endl;
    map[0][0] = '1';
    for (int i = 0; i < strlen(a); i++) 
	{
        switch (a[i]) 
		{
            case 'w':
                if (x != 0 && map[x - 1][y] != '#'&&map[x - 1][y] != '1') 
				{
                    x = x - 1;
                    map[x][y]='1';
                }
                break;
            case 'a':
                if (y != 0 && map[x][y - 1] != '#'&&map[x][y-1] != '1') 
				{
                    y = y - 1;
                    map[x][y]='1';
                }
                break;
            case 's':
                if (x != 2 && map[x + 1][y] != '#'&&map[x+1][y] != '1') 
				{
                    x = x + 1;
                    map[x][y]='1';
                }
                break;
            case 'd':
                if (y != 3 && map[x][y + 1] != '#'&&map[x][y+1] != '1') 
				{
                    y = y + 1;
                    map[x][y]='1';
                }
                break;
        }
    }
    map[x][y] = 'A';
    for (int i = 0; i <= 2; i++) 
	{
        for (int j = 0; j <= 3; j++) 
		{
            cout << map[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|DiscuzX

GMT+8, 2025-2-13 02:27 , Processed in 0.049773 second(s), 26 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.. 技术支持 by 巅峰设计

快速回复 返回顶部 返回列表