codeforces 750D New Year and Fireworks【DFS】
题意:烟花绽放时分为n层,每层会前进ti格,当进入下一层是向左右45°分开前进。 问在网格中,有多少网格至少被烟花经过一次?
题解:最多30层,每层最多前进5格,烟花的活动半径最大为150,每一层的方向都可以由上一层决定,所以一定 小于300*300中情况,直接暴力绽放的过程就行了。bfs和dfs都可以做。
dfs:
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<algorithm>
#include<string> typedef long long ll;
#define MAXN 100000
using namespace std; int n, a[35], m[305][305], vis[35][305][305][8];
//clockwise
int dx[8] = { 1, 1, 0, -1, -1, -1, 0, 1 };
int dy[8] = { 0, -1, -1, -1, 0, 1, 1, 1 }; void dfs(int cur, int x, int y, int dir)
{
if (cur == n || vis[cur][x][y][dir])
return;
vis[cur][x][y][dir] = m[x][y] = 1;
for (int i = 1; i < a[cur]; i++)
{
x += dx[dir];
y += dy[dir];
m[x][y] = 1;
}
int nd = (dir + 1) & 7;
dfs(cur + 1, x + dx[nd], y + dy[nd], nd);
nd = (dir + 7) & 7;
dfs(cur + 1, x + dx[nd], y + dy[nd], nd);
} int main()
{
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
dfs(0, 150, 150, 4);
int cnt = 0;
for (int i = 0; i < 305; i++)
{
for (int j = 0; j < 305; j++)
{
if (m[i][j])
cnt++;
}
}
cout << cnt << endl;
return 0;
}
bfs:
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 310;
int mark[maxn][maxn][32][6];
int n,a[32],map[maxn][maxn];
int asp[][2]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};
struct node
{
int x,y,cnt,dir; //cnt:烟花的层数 dir:烟花前进的方向
}temp; void bfs()
{
queue<node> q;
temp.x=150; temp.y=150; temp.cnt=1; temp.dir=0;
q.push(temp);
mark[150][150][1][0]=1;
while(!q.empty())
{
temp=q.front();
q.pop();
int nx=temp.x, ny=temp.y, num=temp.cnt, k=temp.dir;
for(int i=1;i<=a[num];++i)//当前层走的路径
{
nx+=asp[k][0];
ny+=asp[k][1];
map[nx][ny]=1;
}
if(num!=n)
{
int kl=(k-1+8)%8, kr=(k+1)%8;//当前烟花炸裂后,分开的两个子烟花前进的方向
temp.x=nx; temp.y=ny; temp.cnt=num+1;
if(!mark[nx][ny][num+1][kl])//kl左45°
{
mark[nx][ny][num+1][kl]=1;
temp.dir=kl;
q.push(temp);
}
if(!mark[nx][ny][num+1][kr])//kr右45°
{
mark[nx][ny][num+1][kr]=1;
temp.dir=kr;
q.push(temp);
}
}
}
int ans=0;
for(int i=0;i<=300;++i)
for(int j=0;j<=300;++j)
if(map[i][j])
ans++;
printf("%d\n",ans);
} int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(map,0,sizeof(map));
memset(mark,0,sizeof(mark));
for(int i=1;i<=n;++i)
scanf("%d",&a[i]);
bfs();
}
return 0;
}
codeforces 750D New Year and Fireworks【DFS】的更多相关文章
- Codeforces 445 A DZY Loves Chessboard【DFS】
题意:给出n*m的棋盘,在‘.’处放上B或者W,最后要求所有的B和W都不相邻 先把棋盘的点转化成‘B’,再搜,如果它的四周存在‘B’,则将它变成'W' 一直挂在第五个数据的原因是,没有dfs(nx,n ...
- Codeforces 475 B Strongly Connected City【DFS】
题意:给出n行m列的十字路口,<代表从东向西,>从西向东,v从北向南,^从南向北,问在任意一个十字路口是否都能走到其他任意的十字路口 四个方向搜,搜完之后,判断每个点能够访问的点的数目是否 ...
- Codeforces 510B Fox And Two Dots 【DFS】
好久好久,都没有写过搜索了,看了下最近在CF上有一道DFS水题 = = 数据量很小,爆搜一下也可以过 额外注意的就是防止往回搜索需要做一个判断. Source code: //#pragma comm ...
- 【dfs】codeforces Journey
http://codeforces.com/contest/839/problem/C [AC] #include<iostream> #include<cstdio> #in ...
- 【DFS】codeforces B. Sagheer, the Hausmeister
http://codeforces.com/contest/812/problem/B [题意] 有一个n*m的棋盘,每个小格子有0或1两种状态,现在要把所有的1都变成0,问最少的步数是多少?初始位置 ...
- 【第40套模拟题】【noip2011_mayan】解题报告【map】【数论】【dfs】
目录:1.潜伏者 [map] 2.Hankson的趣味题[数论]3.mayan游戏[dfs] 题目: 1. 潜伏者(spy.pas/c/cpp)[问题描述]R 国和S 国正陷入战火之中,双方都互派间谍 ...
- Kattis - glitchbot 【DFS】
Kattis - glitchbot [DFS] 题意 有一个机器人 刚开始在(0, 0),然后给出一个目标点,并且会给出一系列指令,但是其中会有一个指令是错误的.我们需要找出那个指令,并且改成正确的 ...
- HDU 6113 度度熊的01世界 【DFS】(2017"百度之星"程序设计大赛 - 初赛(A))
度度熊的01世界 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- 【dfs】P1331 海战
题目描述 在峰会期间,武装部队得处于高度戒备.警察将监视每一条大街,军队将保卫建筑物,领空将布满了F-2003飞机.此外,巡洋船只和舰队将被派去保护海岸线.不幸的是因为种种原因,国防海军部仅有很少的几 ...
随机推荐
- CodeForces - 615D Multipliers(数论)
http://codeforces.com/problemset/problem/615/D 题意 给出m个质因子,组成一个数n.问n的约数的乘积是多少,输出mod 1e+7的结果. 分析 从输入我们 ...
- Codeforces 950 D. A Leapfrog in the Array
http://codeforces.com/contest/950/problem/D 前n/2个格子的奇数下标的数没有参与移动 候n/2个格子的奇数下标的数一定是一路移向偶数下标移 所以还原数的初始 ...
- android数据库简单操作
1.DbOpenHelper package com.example.dbtest.dbHelper; import android.content.Context; import android.d ...
- 第18月第22天 机器学习first
1.网易公开课 机器学习 http://open.163.com/special/opencourse/machinelearning.html https://github.com/search ...
- tr 设置margin、padding无效
tr.td设置margin 无效 tr 设置padding无效.td设置padding有效
- 2018-2019 前期任务(一):资料阅读&Python入门
2018-2019 前期任务(一):资料阅读&Python入门 资料原文地址:Dumbcoin - An educational python implementation of a bitc ...
- 改变checkbox的默认样式
针对于CheckBox默认样式的改变,和选中状态的改变 <label class="checkBox"><input type="checkbox&qu ...
- Jetson tk1 刷机教程
前期准备: 1. Jetson TK1开发板. 2. 安装有ubuntu系统的PC(或者ubuntu虚拟机)切记:不管是PC还是虚拟机,务必确保有大于5G的存储空间,之后安装过程会作详细解释. 3 ...
- python 历险记(一)— python 的String,集合(List,元组,Dict)
目录 引言 String 有哪些有用的方法? 如何拼接字符串? 如何分隔字符串? 如何获取字符串长度 如何将 list 拼接成字符串? 如何替换字符串? 如何去除字符串中的空格? 如何子字符串是否包含 ...
- 根据txt中的文件名将文件复制到目标文件夹中
功能如标题,之所以这么做是有的时候文件数目较多,一个一个复制太复杂了,代码如下: # -*- coding:utf-8 -*- #2018_03_18 #实现功能:根据文件名字将对应的文件复制到目标地 ...