题目:https://loj.ac/problem/2550

只会写20分的搜索……

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
int n,m,ans;
bool b[N][N],vis[N][N];
void dfs(int x,int y,bool fx,int lj)
{
if(y>m)y=; if(x>n)x=;
if(vis[x][y])
{
if(x==&&y==)
{
bool fg=;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
if(!vis[i][j]){fg=;break;}
if(!fg)ans+=lj;
}
return;
}
if(b[x][y])fx=; if(!fx)lj++;
vis[x][y]=;
dfs(x+,y,fx,lj); dfs(x,y+,fx,lj);
vis[x][y]=;
}
int main()
{
int T;scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
scanf("%1d",&b[i][j]);
ans=;
dfs(,,,);
printf("%d\n",ans);
}
return ;
}

观察多篇题解:

https://blog.csdn.net/qq_39972971/article/details/80441415

https://www.cnblogs.com/cjyyb/p/10422074.html

https://blog.csdn.net/scar_lyw/article/details/80411617

由结论可知合法的方案取决于左上角 d*d 怎么决策。(副对角线可以拐,所以是 d 条而不是 2*d-1 条)

枚举 d*d 里向下 i 步,向右 j=d-i 步,那么需要 i 和 n 互质、 j 和 m 互质。这样就是合法方案。考虑已知 i , j ,算贡献。

每个位置 ( x, y ) 都会在 “一轮”(d步) 之后走到 ( x+i , y+j ) 。

( 1, 1 ) 位置第一轮走到 ( i+1 , j+1 ) 。考虑 DP 这个第一轮的走法,就知道全局的走法了。

( 1, 1 ) 只能向下走或向右走。走过位置 ( x, y ) ,意味着会在之后的轮中把 ( x+k*i , y+k*j ) 都走过。

把 “走到第一个障碍为止的步数” 改成 “走到每个障碍为止的步数中的最小值” , 一个位置 ( x, y ) 的权值就是所有 ( x+k*i , y+k*j ) 的是障碍的点的 “走到该点的步数最小值” 取 min ;

就是要 DP 一条从 ( 1, 1 ) 到 ( i+1 , j+1 ) 的只能向下或向右走的路径,该路径贡献是路径上各点权值的最小值。

#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
int Mn(int a,int b){return a<b?a:b;}
int gcd(int a,int b){return b?gcd(b,a%b):a;}
const int N=,M=N*N,mod=;
int upt(int x){while(x>=mod)x-=mod;while(x<)x+=mod;return x;} int n,m,lm,c[N][N],dp[N][N][M],ans;
bool b[N][N];
void cz(int &x,int y){x=upt(x+y);}
void solve(int x,int y)
{
memset(dp,,sizeof dp);
dp[][][c[][]]=;
for(int i=;i<=x+;i++)
for(int j=;j<=y+;j++)
for(int k=;k<=lm;k++)
{
int tp=dp[i][j][k]; if(!tp)continue;
if(i<=x)cz(dp[i+][j][Mn(k,c[i+][j])],tp);
if(j<=y)cz(dp[i][j+][Mn(k,c[i][j+])],tp);
}
for(int k=;k<=lm;k++)
ans=(ans+(ll)k*dp[x+][y+][k])%mod;
}
int main()
{
int T;scanf("%d",&T);
while(T--)
{
ans=;
scanf("%d%d",&n,&m); lm=n*m;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
scanf("%1d",&b[i][j]);
int g=gcd(n,m);
for(int x=;x<g;x++)
{
int y=g-x;
if(gcd(x,n)!=||gcd(y,m)!=)continue;
for(int i=;i<=x+;i++)
for(int j=;j<=y+;j++)
{
int d=i+j-;
if(b[i][j]){c[i][j]=d;continue;}
int tx=i+x, ty=j+y; d+=g;
if(tx>n)tx-=n; if(ty>m)ty-=m;
while()
{
if(b[tx][ty]||(tx==i&&ty==j))
{c[i][j]=d;break;}
tx+=x; ty+=y; d+=g;
if(tx>n)tx-=n;if(ty>m)ty-=m;
}
}
solve(x,y);
}
printf("%d\n",ans);
}
return ;
}

LOJ 2550 「JSOI2018」机器人——找规律+DP的更多相关文章

  1. 【LOJ】#2550. 「JSOI2018」机器人

    题解 我不会打表找规律啊QAQ 规律就是 对于\(n = m\)我们每一条左下到右上的对角线上的点的走法都是一样的且每n步一个轮重复 对于\(n != m\)我们找到最大公约数\(d\),在每个\(d ...

  2. LOJ 2546 「JSOI2018」潜入行动——树形DP

    题目:https://loj.ac/problem/2546 dp[ i ][ j ][ 0/1 ][ 0/1 ] 表示 i 子树,用 j 个点,是否用 i , i 是否被覆盖. 注意 s1<= ...

  3. LOJ 2548 「JSOI2018」绝地反击 ——二分图匹配+网络流手动退流

    题目:https://loj.ac/problem/2548 如果知道正多边形的顶点,就是二分答案.二分图匹配.于是写了个暴力枚举多边形顶点的,还很愚蠢地把第一个顶点枚举到 2*pi ,其实只要 \( ...

  4. LOJ 2551 「JSOI2018」列队——主席树+二分

    题目:https://loj.ac/problem/2551 答案是排序后依次走到 K ~ K+r-l . 想维护一个区间排序后的结果,使得可以在上面二分.求和:二分可以知道贡献是正还是负. 于是想用 ...

  5. LOJ 2547 「JSOI2018」防御网络——思路+环DP

    题目:https://loj.ac/problem/2547 一条树边 cr->v 会被计算 ( n-siz[v] ) * siz[v] 次.一条环边会被计算几次呢?于是去写了斯坦纳树. #in ...

  6. @loj - 3157@「NOI2019」机器人

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 小 R 喜欢研究机器人. 最近,小 R 新研制出了两种机器人,分 ...

  7. 「JSOI2018」机器人

    在本题当中为了方便,我们将坐标范围改至 \((0 \sim n - 1, 0 \sim m - 1)\),行走即可视作任意一维在模意义下 \(+1\). 同时,注意到一个位置只能经过一次,则可以令 \ ...

  8. LOJ 3092 「BJOI2019」排兵布阵 ——DP

    题目:https://loj.ac/problem/3092 同一个人的不同城堡之间没有什么联系,只是和<=m.所以对每个城堡的 s 个值排序,做一个 f[ i ][ j ] 表示第 i 个城堡 ...

  9. Loj #3059. 「HNOI2019」序列

    Loj #3059. 「HNOI2019」序列 给定一个长度为 \(n\) 的序列 \(A_1, \ldots , A_n\),以及 \(m\) 个操作,每个操作将一个 \(A_i\) 修改为 \(k ...

随机推荐

  1. k-means缺陷

    k均值算法非常简单且使用广泛,但是存在的缺陷有: 1. K值需要预先给定: 属于预先知识,很多情况下K值的估计非常困难. 2. K-Means算法对初始选取的聚类中心点是敏感的: 不同的随机种子点得到 ...

  2. 移动端设备中1px适配

    方式1:伪类+transform实现,主要用transform中的scale缩放,缩放默认中心点是以x,y轴的50%处,因此需要用transform-origin调整中心点 html代码: <d ...

  3. [RESTful] DHC Client

    安装Chrome的DHC插件, 进入DHC Client谷歌插件网页. 安装到Chrome浏览器: 点击Chrome设置 点击扩展程序 把刚刚下载的文件解压缩 把 .crx 后缀的文件直接拖入Chro ...

  4. The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.

    好久没有冒泡了,最近在新环境上搭建应用时,启动报错: INFO: Illegal access: this web application instance has been stopped alre ...

  5. git和github的第一次接触

    我的github的helloworld链接: https://github.com/xuziqian111/hello-world/blob/master/helloworld.java 我的gith ...

  6. 运维ldd语法--》ldconfig

    Linux:ldd命令详解   ldd 用于打印程序或者库文件所依赖的共享库列表. 语法 ldd(选项)(参数) 选项 --version:打印指令版本号: -v:详细信息模式,打印所有相关信息: - ...

  7. 2018—自学Selenium+Python 笔记(一)

    在开始学习前,先唠几句: 身为一个开发人员,为何想要转测试..很多人不解. 但我觉得这并没有什么不可,测试人员是质量的把控者: 要出一个让客户满意的产品,单纯靠开发自测,是不够的..相信其中缘由大家都 ...

  8. WEB学习笔记3-开发环境和工具

    WEB前端集成开发环境:Aptana Studio和WebStormWEB前端代码调试:IE浏览器自带的IE Dev Toolbar,Chrome浏览器自带的Developer Tools,Firef ...

  9. 阿里云ecs使用补充说明

    前置文章:http://www.cnblogs.com/newbob/articles/5400495.html 阿里云ecs使用心得的一些个人补充部分 一.在yum安装node/npm的过程中出现的 ...

  10. Python:从入门到实践--第十一章--测试代码--练习

    #1.城市和国家:编写一个函数,它接受两个形参:一个城市名和一个国家名. #这个函数返回一个格式为City,Country的字符串,如Santiago,Chile.将这个函数 #存储在一个名为city ...