200326题(999.车的可用捕获量)

在这里插入图片描述
这题很无聊的地方在于:以往北走为例,如果在往北走的过程中遇到一个卒,就不能再继续往北,而是要重新回到R的原点并重新选择新的方向去寻找。

class Solution {
public:
	int numRookCaptures(vector<vector<char>>& board) {
		int pos_x = -1, pos_y = -1;//找R
		for (int i = 0; i < 8; i++)
			for (int j = 0; j < 8; j++)
			{
				if (board[i][j] == 'R')
				{
					pos_x = i;
					pos_y = j;
					break;
				}

			}

		if (pos_x == -1)return 0;//没找到R
								 //start
		int dx[] = { 0,0,-1,1 };//上下左右
		int dy[] = { 1,-1,0,0 };//上下左右
		int res = 0;
		for (int i = 0; i < 4; i++)
		{
			int temp_x = pos_x, temp_y = pos_y;//R回到初始位置

			while (temp_x >= 0 && temp_x <= 7 && temp_y >= 0 && temp_y <= 7)
			{
				if (board[temp_x][temp_y] == 'B')break;

				if (board[temp_x][temp_y] == 'p')
				{
					res++;
					break;
				}
				temp_x += dx[i];
				temp_y += dy[i];

			}

		}
		return res;
	}

};
发布了241 篇原创文章 · 获赞 4 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/ShenHang_/article/details/105116986