nyoj 302
解題思路:
首先這道題目我一開始沒(méi)看懂題意,想直接尋找遞推關(guān)系,但后來(lái)感覺如果這么去找遞推關(guān)系會(huì)陷入死循環(huán)的。。后面參考了別人的解題報(bào)告。。先把這個(gè)題目的大意再?gòu)?fù)述一下:有一個(gè)m*n的矩陣,每個(gè)矩陣上的編號(hào)代表著國(guó)家編號(hào),相同編號(hào)之間是有傳送門,而相鄰的點(diǎn)(上下左右)之間是可達(dá)的,問(wèn)從(1,1)到(m,n)的方案數(shù)。。由于每個(gè)點(diǎn)之間肯定會(huì)有來(lái)回走的情況,直接找遞推是非常難的。。。
別人是這么想的:把m*n的點(diǎn)映射到一維坐標(biāo)上,再根據(jù)根據(jù)這些點(diǎn)是否可達(dá)建立一個(gè)鄰接矩陣,最后再p次冪即可。。。
以后在這種矩陣中尋找路徑方案數(shù)的問(wèn)題中,如果題目中沒(méi)有出現(xiàn)可達(dá)矩陣,那么可以先把矩陣中所有點(diǎn)映射到一維坐標(biāo)上來(lái),再根據(jù)點(diǎn)與點(diǎn)之間的情況建立可達(dá)矩陣,最后再做可達(dá)矩陣的冪運(yùn)算。。
AC:
#include <iostream> #include <cstdio> #include <cstring> using namespace std; #define LL long longconst int N = 105; const LL mod = 1000007; struct Matrix {int n;LL a[N][N];Matrix(){memset(a, 0, sizeof(a));} } ans, A; int node[N], n; int m, p;bool Judge(int x, int y) {if(x - n == y) return true;else if(x + n == y) return true;else if(x - 1 == y && y % n != 0) return true;else if(x + 1 == y && x % n != 0) return true;else return false; }Matrix operator * (Matrix a, Matrix b) {Matrix tmpans;tmpans.n = a.n;for(int i = 1; i <= a.n; i ++){for(int j = 1; j <= a.n; j ++){for(int k = 1; k <= a.n; k ++)tmpans.a[i][j] = (tmpans.a[i][j] + a.a[i][k] * b.a[k][j]) % mod;}}return tmpans; }void power(int k) {while(k){if(k & 1) ans = ans * A;A = A * A;k = k >> 1;} }int main() {int T;cin >> T;while(T --){cin >> n >> m >> p;for(int i = 0; i < m; i ++) <span style="white-space:pre"> </span>{for(int j = 1; j <= n; j ++){cin >> node[i * n + j];}}for(int i = 1; i <= m * n; i ++){for(int j = 1; j <= m * n; j ++){A.a[i][j] = 0; ans.a[i][j] = 0;if(i == j) continue;if(node[i] == node[j]) A.a[i][j] = 1;else if(Judge(i, j)){A.a[i][j] = 1;}}ans.a[i][i] = 1;}ans.n = n * m; A.n = n * m;power(p);printf("%lld\n", ans.a[1][n * m] % mod);}return 0; }
總結(jié)
- 上一篇: jeecg公开课今晚主题:新版本功能介绍
- 下一篇: hdu 1532(最大流)