AtCoder Grand Contest 043 A Range Flip Find Route dp(动归)

AtCoder Grand Contest 043   比赛人数3492   慢,比赛开始后8分钟看到第一题,比赛开始后11分钟看到所有题

AtCoder Grand Contest 043   A   Range Flip Find Route   dp(动归)

总目录详见https://blog.csdn.net/mrcrack/article/details/104454762

在线测评地址https://atcoder.jp/contests/agc043/tasks/agc043_a

在一般广搜,深搜上改编的题目,已然面目全非,难度陡增。

理解该题,请结合代码,模拟样例数据,就能很快明白

3 3
.##
.#.
##.

1


0 1 1 
0 1 2 
1 1 2 
2 2
#.
.#

2


1 2 
2 3 


4 4
..##
#...
###.
###.

0 0 1 1 
1 0 0 0 
1 1 1 0 
1 1 1 0 
5 5
.#.#.
#.#.#
.#.#.
#.#.#
.#.#.

0 1 2 3 4 
1 2 3 4 5 
2 3 4 5 6 
3 4 5 6 7 
4 5 6 7 8

AC代码如下

#include <cstdio>
#include <algorithm>
#define maxn 105
using namespace std;
int dp[maxn][maxn];
char s[maxn][maxn];
int main(){
	int h,w,i,j;
	scanf("%d%d",&h,&w);
	for(i=1;i<=h;i++)scanf("%s",s[i]+1);
	for(i=1;i<=h;i++)
		for(j=1;j<=w;j++)
			dp[i][j]=210;//设置最大值
	dp[1][1]=(s[1][1]=='#');
	for(i=1;i<=h;i++)
		for(j=1;j<=w;j++){
			dp[i][j+1]=min(dp[i][j+1],dp[i][j]+(s[i][j]!=s[i][j+1]));//(i,j)右侧
			dp[i+1][j]=min(dp[i+1][j],dp[i][j]+(s[i][j]!=s[i+1][j]));//(i,j)下方
		}
	printf("%d\n",(dp[h][w]+1)/2);//.=>#=>.程序中变换了2次,但我们看到的是变换了1次.=>#
	return 0;
}
发布了620 篇原创文章 · 获赞 550 · 访问量 46万+

猜你喜欢

转载自blog.csdn.net/mrcrack/article/details/105022045