I Like Matrix!【DFS】

Description–

Given moves with k: from (i, j) is moved to the (i + xk, j + yk ) (xk, yk> 0). Ask a matrix of n * m,
from (1, 1) start, the number of positions can be reached.


Input–

The first line contains three integers n, m and k.
After two lines contains k xi and yi.

Output–

Total line contains an integer ans, indicates the number of positions that can be reached.


Sample Input–

5 5 2
2 1
1 3

Sample Output–

5


Notes -

To 100% of the data: n, m ≤ 100, k ≤ 10


Code -

#include<iostream>
#include<cstdio>
using namespace std;
struct fy
{
	int xk,yk;
}a[15];
int n,m,k,ans;
bool pd[105][105];
void dfs(int x,int y)
{
	if (pd[x][y]) return ;
	pd[x][y]=1;
	for (int i=1;i<=k;++i)
	  if (x+a[i].xk<=n && y+a[i].yk<=m)
		dfs(x+a[i].xk,y+a[i].yk);
}
int main()
{
	scanf("%d%d%d",&n,&m,&k);
	for (int i=1;i<=k;++i)
      scanf("%d%d",&a[i].xk,&a[i].yk);
    dfs(1,1);
    for (int i=1;i<=n;++i)
      for (int j=1;j<=m;++j)
        if (pd[i][j]) ans++;
	printf("%d",ans);
	
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_43654542/article/details/90698897