1. 程式人生 > >藍橋杯 蘭頓螞蟻 C++實現

藍橋杯 蘭頓螞蟻 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