upc组队训练第十九场

问题 L: Spiral Matrix
时间限制: 1 Sec 内存限制: 128 MB

题目描述
Lee got a ticket to the Google Developer Day. When he came to the exhibition hall, he found that the booths were located in a matrix of size n × m.
Unfortunately, Lee had badly sprained his left ankle a week before the GDD when playing basketball. As a result, he couldn’t wander freely as he wanted. His ankle got hurt if he tried to turn left. Lee also didn’t want to make a big right turn by turning right multiple times, which made him look stupid.
Lee wanted to visit each booth exactly once. Given his ankle situation, he made some rules visiting the booths. He started with any booth in the exhibition hall, and chose an initial direction. Each move must follow one of the possible moves:

  1. Go straight to visit the next booth.
  2. Turn right once and then go straight to visit the next booth.
    You are a friend of Lee and you have the good habit to help him. Can you find out the number of different ways to visit each booth exactly once? Two ways are considered different if and only if the visiting orders in these two ways vary.
    输入
    The first line of the input gives the number of test cases, T (1 ≤ T ≤ 100). T test cases follow.
    For each test case, the first line contains two integers n and m (1 ≤ n, m ≤ 100), the number of rows and the number of columns of the matrix.
    输出
    For each test case, output one line containing “Case #x: y”, where x is the test case number (starting from 1), y is the number of different ways modulo (109+ 7).
    样例输入 Copy
    1
    2 2
    样例输出 Copy
    Case #1: 4
    提示
    Here are the 4 different ways for n = 2, m = 2.
    在这里插入图片描述

题意:
大概是说有一个人想要参观所有的摊位,但是他只能直走或者右转一下然后直行,他可以从任意一个摊位开始,问你有多少种方案可以参观完所有摊位
这个题仔细一想其实是没办法dfs的,然后也不知道怎么了就觉得这是个规律题,于是三个人就开始找规律了…

我们很容易就可以想到,只有一行的时候,只有两种方案,从左边开始直行或者从右边开始直行
有两行的时候,他可以和提示中一样每次更换起点顺时针走,方案数是 n * m
然后我们顺着找了其他情况时候的方案数
最后发现这是一个类似乘法表一样的东西,这样就很容易就可以找到规律了

在这里插入图片描述
代码在这里

int T;
int f[110][110];

int main(){
	
	f[1][1] = 1;
	for(int i=2;i<=100;i++){
		for(int j=1;j<=i;j++){
			if(j == 1 && i != 1) f[i][j] = 2;
			else if(j == 2) f[i][j] = i*j % mod;
			else f[i][j] = (f[i][j-1] + 2) % mod;
		}
	}
	
	int k = 0;
	scanf("%d",&T);
	while(T--){
		
		int m,n;
		m = read, n = read;
		if(m > n) swap(m,n);
		printf("Case #%d: %d\n",++k,f[n][m]);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你数过天上的星星吗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值