蓝桥杯 兰顿蚂蚁 C++实现

今天看到蓝桥杯的竞赛题:兰顿蚂蚁,感觉不怎么难,刚好想写一写代码,由于是小白,代码的不规范之处还请指出来。原创:苏州大学,右京先生。曹柒夏。https://blog.csdn.net/jwsl999/article/details/85107483
前情提要:
在这里插入图片描述

#include <iostream>
using namespace std;
//定义上下左右方向,1代表黑格,0代表白格。
typedef enum _direction{Dup,Dright,Ddown,Dleft}Direction;
void SolveNextDirection(Direction  direction, int **arr, int nRow, int nCol,int move);
int main()
{
	int m, n;
	int nRow, nCol, move;
	char Dre;
	Direction direction;
	cin >> m >> n;
	int **arr = new int *[m];
	for (int i = 0; i < m; ++i)
	{
		arr[i] = new int[n];
	}
	for (int i = 0; i < m;++i)
		for(int j = 0; j < n; ++j)
		{
			cin >> arr[i][j];
		}
	cin >> nRow >> nCol >> Dre >> move;
	if (Dre == 'U')
	{
		direction = Dup;
	}
	else if (Dre == 'R')
	{
		direction = Dright;
	}
	else if (Dre == 'D')
	{
		direction = Ddown;
	}
	else
		direction = Dleft;
	
	SolveNextDirection(direction,arr,nRow,nCol,move);
	delete[] arr;
	return 0;
}
void SolveNextDirection(Direction direction,int **arr,int nRow,int nCol,int move)
{
	int n = 0;
	while (n < move)
	{
		if (arr[nRow][nCol] == 1)
		{
			if (direction == Dleft)
			{
				direction = Dup;
			}
			else
				direction = (Direction)(direction + 1);

			arr[nRow][nCol] = 0;
		}
		else
		{
			if (direction == Dup)
			{
				direction = Dleft;
			}
			else
				direction = (Direction)(direction - 1);
			arr[nRow][nCol] = 1;
		}
		if (direction == Dup)
			nRow -= 1;
		else if (direction == Dright)
			nCol += 1;
		else if (direction == Ddown)
			nRow += 1;
		else
			nCol -= 1;
		++n;
	}
	
	cout << nRow << " " << nCol << endl;
}

日志原创,转载请标明出处!
https://blog.csdn.net/jwsl999/article/details/85107483

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值