洛谷-1002-马拦过河卒

只过了1/5
另外我觉得这个办法也挺不优雅的,效率估计也不是很高

int main() {

	 int x1, y1, x2, y2;
	 cin >> x1 >> y1 >> x2 >> y2;

	 vector<pair<int, int>> horse;
	 // 要判断马的坐标和马可以到达的坐标是否合法
	 if (x2 <= x1 && y2 <= y1) horse.push_back(make_pair(x2, y2));
	 if (y2 - 2 >= 0) {
		 if (x2 - 1 >= 0) horse.push_back(make_pair(x2 - 1, y2 - 2));
		 if (x2 + 1 <= x1) horse.push_back(make_pair(x2 + 1, y2 - 2));
	 }
	 if (y2 - 1 >= 0) {
		 if (x2 - 2 >= 0) horse.push_back(make_pair(x2 - 2, y2 - 1));
		 if (x2 + 2 <= x1) horse.push_back(make_pair(x2 + 2, y2 - 1));
	 }
	 if (y2 + 2 <= y1) {
		 if (x2 - 1 >= 0) horse.push_back(make_pair(x2 - 1, y2 + 2));
		 if (x2 + 1 <= x1) horse.push_back(make_pair(x2 + 1, y2 + 2));
	 }
	 if (y2 + 1 >= 0) {
		 if (x2 - 2 >= 0) horse.push_back(make_pair(x2 - 2, y2 + 1));
		 if (x2 + 2 <= x1) horse.push_back(make_pair(x2 + 2, y2 + 1));
	 }
	 //// 打印马的控制点
	 //for (pair<int, int> p : horse) {
		// cout << "("<<p.first <<"," << p.second<<")" << endl;
	 //}

	 vector<vector<int>> steps(y1+1, vector<int>(x1+1,0));
	 steps[0][0]=1;
	 // 初始化棋盘边界值
	 for (int i = 1; i <= x1; i++)
		 if (count(horse.begin(),horse.end(),make_pair(0,i))) break;
		 else steps[0][i] = 1;
	 for (int j = 1; j <= y1; j++) 
		 if (count(horse.begin(), horse.end(), make_pair(j, 0))) break;
		 else steps[j][0] = 1;

	 for (int i = 1; i <= y1; i++) {
		 // 如果遇到被马占领的点,就置零
		 // 如果被置零了,那么肯定就会选择另一个方向过来,都是零的话代表此点不通
		 for (int j = 1; j <= x1; j++) {
			 if (count(horse.begin(), horse.end(), make_pair(i, j))) steps[i][j] = 0;
			 else steps[i][j] = steps[i - 1][j]+ steps[i][j - 1];
		 }
	 }

	 //// 打印棋盘
	 //for (vector<int>ss : steps) {
		// for (int s : ss) {
		//	 cout << s << " ";
		// }
		// cout << endl;
	 //}

	 cout << steps[x1][ y1];

	 return 0;
 }
posted @ 2022-09-03 13:17  YaosGHC  阅读(29)  评论(0)    收藏  举报