主题链接:点击打开链接

意甲冠军:

特定n*m矩阵

# 是墙 . 和字母是平地

最多有26个字母(不反复出现)

以下k个指令。

每一个指令代表移动的方向和步数。

若以某个字母为起点,依次运行全部的指令,不论什么过程都不会撞到墙或走出地图。则这个字母合法。

按字典序输出全部合法的字母。若没有字母合法则输出' no solution'

预处理一下前缀和然后暴力。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <vector>
#include <map>
using namespace std;
#define N 1005
vector<char>ans;
struct node{
int x, y;
char c;
void put(){printf("(%d,%d) : %c\n", x, y, c);}
}a[30];
int step[4][2] = {-1,0, 1,0, 0,-1, 0,1};
int work[100005][2];
char s[N];
int mp[N][N], n, m, k, top, h[N][N], l[N][N];
bool okh(int H, int x, int y){return h[H][y]-h[H][x-1] == 0;}
bool okl(int L, int x, int y){return l[L][y]-l[L][x-1] == 0;}
bool ok(int x, int y, int i, int j){
if(x>i)swap(x,i); if(y>j)swap(y,j);
if(x==i)
return okh(x, y, j);
else
return okl(y, x, i);
}
bool inmap(int x, int y){return 1<=x&&x<=n&&1<=y&&y<=m;}
bool judge(int x, int y){
for(int i = 0; i < k; i++) {
int ux = step[work[i][0]][0] * work[i][1] + x, uy = step[work[i][0]][1] *work[i][1] + y;
if(!inmap(ux,uy))return false;
if(!ok(x, y, ux, uy))return false;
x = ux; y = uy;
}
return true;
}
void debug(){for(int i = 0; i < top; i++)a[i].put();}
void solve(){
// debug();
ans.clear();
for(int i = 0; i < top; i++)
if(judge(a[i].x, a[i].y))
ans.push_back(a[i].c);
if(ans.size()==0){
puts("no solution");
return ;
}
sort(ans.begin(), ans.end());
for(int i = 0; i < ans.size(); i++)printf("%c",ans[i]);
puts("");
}
void input(){
top = 0;
memset(mp, 0, sizeof mp);
memset(h, 0, sizeof h);
memset(l, 0, sizeof l);
for(int i = 1; i <= n; i++){
scanf("%s", s+1);
for(int j = 1; j <= m; j++)
{
if(s[j]=='#')
{
mp[i][j] = 1;
h[i][j] = 1;
l[j][i] = 1;
}
else if('A'<=s[j] && s[j]<='Z'){
a[top].x = i; a[top].y = j; a[top].c = s[j];
top++;
}
}
}
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
h[i][j] += h[i][j-1];
for(int i = 1; i <= m; i++)
for(int j = 1; j <= n; j++)
l[i][j] += l[i][j-1];
scanf("%d",&k);
for(int i = 0; i < k; i++){
scanf("%s %d", s, &work[i][1]);
if(s[0] == 'N') work[i][0] = 0;
else if(s[0]=='S') work[i][0] = 1;
else if(s[0]=='W') work[i][0] = 2;
else work[i][0] = 3;
}
}
int main(){
while(~scanf("%d %d",&n,&m)){
input();
solve();
}
return 0;
}

版权声明:本文博主原创文章。博客,未经同意不得转载。

Codeforces 106D Treasure Island 预处理前缀+暴力(水的更多相关文章

  1. [Codeforces 1214D]Treasure Island(dfs)

    [Codeforces 1214D]Treasure Island(dfs) 题面 给出一个n*m的字符矩阵,'.'表示能通过,'#'表示不能通过.每步可以往下或往右走.问至少把多少个'.'变成'#' ...

  2. CodeForces 838A Binary Blocks(前缀和)题解

    题意:给你个n*m的矩阵,要求你找到一个k,k > 1,使得矩阵可以分为很多k * k的小正方形,然后进行操作把每个小正方形都变为0或1,问你怎样使操作数最小. 思路:随便暴力不可取,显然你每次 ...

  3. Gym 100971A Treasure Island BFS 思维题

    A - Treasure Island Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64 ...

  4. [Codeforces 1201D]Treasure Hunting(DP)

    [Codeforces 1201D]Treasure Hunting(DP) 题面 有一个n*m的方格,方格上有k个宝藏,一个人从(1,1)出发,可以向左或者向右走,但不能向下走.给出q个列,在这些列 ...

  5. D. Treasure Island

    D. Treasure Island dfs大法好== 写半天bfs疯狂MLE dfs标记掉路上的一些点 然后再跑一遍dfs #include<bits/stdc++.h> using n ...

  6. Treasure Island DFS +存图

    All of us love treasures, right? That's why young Vasya is heading for a Treasure Island. Treasure I ...

  7. Codeforces Round #280 (Div. 2) A B C 暴力 水 贪心

    A. Vanya and Cubes time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  8. Karen and Coffee CodeForces - 816B (差分数组+预处理前缀和)

    To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...

  9. Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) A B C D 暴力 水 二分 几何

    A. Vicious Keyboard time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. jquery 获取上一个兄弟元素和下一个兄弟元素

    jQuery.prev(),返回上一个兄弟节点,不是所有的兄弟节点 jQuery.prevAll(),返回所有之前的兄弟节点 jQuery.next(),返回下一个兄弟节点,不是所有的兄弟节点 jQu ...

  2. JS学习笔记 - fgm练习 - 输入数字求和 正则replace onkeyup事件

    <style> body{font-size: 12px;} .outer{ width: 500px; margin: 0 auto; } span{ color: #999; } in ...

  3. 使用Surging Mqtt 开发基于WS的MqttClient客户端

    原文:使用Surging Mqtt 开发基于WS的MqttClient客户端 最近一段时间由于要做一套智能设备系统,而有幸了解到Surging中的Mqtt broker,学习了很多东西本篇文章基于Su ...

  4. QWaitCondition 的正确使用方法(通过 mutex 把有严格时序要求的代码保护起来,同时把 wakeAll() 也用同一个 mutex 保护起来)

    简单用法 QWaitCondition 用于多线程的同步,一个线程调用QWaitCondition::wait() 阻塞等待,直到另一个线程调用QWaitCondition::wake() 唤醒才继续 ...

  5. .dmp文件导出使用示例

    exp导出的几种用例,先睹为快: 1 将数据库SampleDB完全导出,用户名system 密码manager 导出到E:/SampleDB.dmp中 exp system/manager@TestD ...

  6. 小型机与pc服务器区别

    按CPU的类型来区分,小型机是基于RISC(精简指令集)架构的专用服务器,而服务器是基于CISC(复杂指令集)架构的PC服务器.小型机相对于普通服务器来说,一直有不可比拟的优势.(1)如必能稳定,它具 ...

  7. jQuery实现多种切换效果的图片切换的五款插件

    1:Nivo SliderNivoslider:丰富的图片切换效果 官方网址:https://themeisle.com/plugins/nivo-slider 查看演示:https://www.he ...

  8. 【例题3-1 UVA - 272 】TEX Quotes

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 大水题. 用一个int记录遇到的是左括号还是右括号. [错的次数] 在这里输入错的次数 [反思] 在这里输入反思 [代码] #inc ...

  9. 【u249】新斯诺克

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 斯诺克又称英式台球,是一种流行的台球运动.在球桌上,台面四角以及两长边中心位置各有一个球洞,使用的球分 ...

  10. irms模拟数据生成及数据分析 分类: H_HISTORY 2015-03-06 14:17 212人阅读 评论(0) 收藏

    一.数据准备 1.每天生成随机一个文本,每小时向文本中追加2次数据,每次10万条 随机数据生成: 2,32  * * * *  bash /mnt/jediael/irms/signalGenerat ...