栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 3315 Runaway Robot

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 3315 Runaway Robot

#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<ctime>#include<iostream>#include<algorithm>#include<string>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<list>#define EPS 1e-9#define MAX(a,b) ((a)>(b)?(a):(b))#define MIN(a,b) ((a)<(b)?(a):(b))#define oo 123456789typedef long long LL;const double PI = acos(-1.0);using namespace std;#define MAXN 210char mp[MAXN][MAXN];int flag[MAXN][MAXN];bool vis[MAXN][MAXN];int n, m;bool yes;int sx, sy, ex, ey;void dfs(int x, int y) {if (x >= n || y >= m) {return;}if (flag[x][y] == 0 || vis[x][y] || yes) {return;}vis[x][y] = true;if (x == ex && y == ey) {yes = true;return;}dfs(x + 1, y);dfs(x, y + 1);}bool check(int a, int b) {int i, j;int x, y;int nx, ny;x = y = 0;while (x < n && y < m) {x += a - 1;y += b - 1;}nx = x - (a - 1);ny = y - (b - 1);if (!(n - 1 >= nx && n - 1 <= x && m - 1 >= ny && m - 1 <= y)) {return false;}ex = n - 1 - nx;ey = m - 1 - ny;if (ex == 0 && ey == 0) {ex = a - 1;ey = b - 1;}for (i = 0; i < a; i++) {for (j = 0; j < b; j++) {if (mp[i][j] == '.') {flag[i][j] = 1;} else {flag[i][j] = 0;}}}x = a - 1;y = b - 1;while (x < n && y < m) {nx = x + a - 1;ny = y + b - 1;for (i = x; i <= nx; i++) {for (j = y; j <= ny; j++) {if (mp[i][j] != '.') {flag[i - x][j - y] = 0;}}}x = nx;y = ny;}yes = false;memset(vis, false, sizeof(vis));sx = sy = 0;dfs(sx, sy);if (yes) {yes = false;memset(vis, false, sizeof(vis));sx = ex;sy = ey;ex = a - 1;ey = b - 1;dfs(sx, sy);return yes;}return false;}int main() {int i, j;int ans;while (~scanf("%d%d", &m, &n)) {memset(mp, '.', sizeof(mp));for (i = 0; i < n; i++) {for (j = 0; j < m; j++) {scanf(" %c", &mp[i][j]);}}ans = oo;for (i = 1; i <= n; i++) {for (j = 1; j <= m; j++) {if (i == 1 || j == 1) {continue;}if (i - 1 + j - 1 < ans && check(i, j)) {ans = i - 1 + j - 1;}}}if (ans == oo) {ans = -1;}printf("%dn", ans);}return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/368027.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号