H - Grid 1

Problem Statement

There is a grid with HH horizontal rows and WW vertical columns. Let (i,j)(i,j) denote the square at the ii-th row from the top and the jj-th column from the left.

For each ii and jj (1iH1≤i≤H1jW1≤j≤W), Square (i,j)(i,j) is described by a character ai,jai,j. If ai,jai,j is ., Square (i,j)(i,j) is an empty square; if ai,jai,j is #, Square (i,j)(i,j) is a wall square. It is guaranteed that Squares (1,1)(1,1) and (H,W)(H,W) are empty squares.

Taro will start from Square (1,1)(1,1) and reach (H,W)(H,W) by repeatedly moving right or down to an adjacent empty square.

Find the number of Taro's paths from Square (1,1)(1,1) to (H,W)(H,W). As the answer can be extremely large, find the count modulo 109+7109+7.

Constraints

  • HH and WW are integers.
  • 2H,W10002≤H,W≤1000
  • ai,jai,j is . or #.
  • Squares (1,1)(1,1) and (H,W)(H,W) are empty squares.

Input

Input is given from Standard Input in the following format:

HH WW
a1,1a1,1a1,Wa1,W
::
aH,1aH,1aH,WaH,W

Output

Print the number of Taro's paths from Square (1,1)(1,1) to (H,W)(H,W), modulo 109+7109+7.


Sample Input 1 Copy

Copy
3 4
...#
.#..
....

Sample Output 1 Copy

Copy
3

There are three paths as follows:


Sample Input 2 Copy

Copy
5 2
..
#.
..
.#
..

Sample Output 2 Copy

Copy
0

There may be no paths.


Sample Input 3 Copy

Copy
5 5
..#..
.....
#...#
.....
..#..

Sample Output 3 Copy

Copy
24

Sample Input 4 Copy

Copy
20 20
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................

Sample Output 4 Copy

Copy
345263555

Be sure to print the count modulo 109+7109+7.

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#define ll              long long
#define pii             pair<int, int>
#define rep(i,a,b)      for(int  i=a;i<=b;i++)
#define dec(i,a,b)      for(int  i=a;i>=b;i--)
using namespace std;
int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979323846;
const double eps = 1e-6;
const int mod = 1e9 + 7;
const int N = 1e3 + 5;
//if(x<0 || x>=r || y<0 || y>=c)

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}
ll gcd(ll m, ll n)
{
    return n == 0 ? m : gcd(n, m % n);
}
ll lcm(ll m, ll n)
{
    return m * n / gcd(m, n);
}
bool prime(int x) {
    if (x < 2) return false;
    for (int i = 2; i * i <= x; ++i) {
        if (x % i == 0) return false;
    }
    return true;
}
ll qpow(ll m, ll k, ll mod)
{
    ll res = 1, t = m;
    while (k)
    {
        if (k & 1)
            res = res * t % mod;
        t = t * t % mod;
        k >>= 1;
    }
    return res;
}
bool vis[N][N];
queue<pii> q;
int cnt[N][N];
int main()
{
    int n, m;
    cin >> n >> m;
    vector<string> g(n);
    for (int i = 0; i < n; i++)
        cin >> g[i];
    q.push({ 0,0 });
    vis[0][0] = 1;
    cnt[0][0] = 1;
    while (!q.empty())
    {
        pii f = q.front();
        q.pop();
        for (int i = 0; i < 2; i++)
        {
            int x = f.first + dir[i][0], y = f.second + dir[i][1];
            if (x >= 0 && x < n && y >= 0 && y < m && g[x][y] == '.')
            {
                cnt[x][y] = (cnt[f.first][f.second] + cnt[x][y]) % mod;
                if (!vis[x][y])
                {
                    vis[x][y] = 1;
                    q.push({ x,y });
                }
            }
        }
    }
    cout << cnt[n - 1][m - 1] << endl;
    return 0;
}

 

posted @ 2020-06-17 11:00  DeaL57  阅读(331)  评论(0编辑  收藏  举报