跳过正文
  1. Posts/

codeforces 377 A maze

·637 字·2 分钟

http://codeforces.com/contest/377/problem/A 题意:给定一个n*m的maze. ‘.’代表空,‘#’代表墙。要求构造一种方案,使得将k个空格填成墙壁后不影响当前的连通性(即没有被填充的空格之间可以相互到达) 思路:一开始想从上往下从左往右构造。错误的认为四个角一定是可以变成墙的。

但其实只要是可能在某条路径上的点,就都不一定可以变成墙。。而四个角显然可以被某条路径经过。

正确的解法很巧妙。以任意一个空格开始跑一遍dfs,设空格一共有sum个,那么就dfs到(sum-k)个。可以做好标记。通过dfs得到的这(sum-k)之间一定是联通的。那么只要填充剩下的就可以了。

代码实现
  1/* ***********************************************
  2Author :111qqz
  3Created Time :2015年12月05日 星期六 14时06分54秒
  4File Name :code/cf/problem/377A.cpp
  5************************************************ */
  6
  7#include <cstdio>
  8#include <cstring>
  9#include <iostream>
 10#include <algorithm>
 11#include <vector>
 12#include <queue>
 13#include <set>
 14#include <map>
 15#include <string>
 16#include <cmath>
 17#include <cstdlib>
 18#include <ctime>
 19#define fst first
 20#define sec second
 21#define lson l,m,rt<<1
 22#define rson m+1,r,rt<<1|1
 23#define ms(a,x) memset(a,x,sizeof(a))
 24typedef long long LL;
 25
 26
 27
 28using namespace std;
 29const double eps = 1E-8;
 30const int dx4[4]={1,0,0,-1};
 31const int dy4[4]={0,-1,1,0};
 32const int inf = 0x3f3f3f3f;
 33const int N=5E2+7;
 34char maze[N][N];
 35bool vis[N][N];
 36int n,m,k;
 37int sx,sy;
 38int sum;
 39int num;
 40bool flag = false;
 41bool ok ( int x,int y)
 42{
 43    if (x>=0&&y>=0&&x<=n-1&&y<=m-1&&maze[x][y]=='.'&&!vis[x][y]) return true;
 44    return false;
 45}
 46
 47void dfs( int x,int y)
 48{
 49    vis[x][y] = true;
 50    maze[x][y]='R';
 51  //  cout<<"x:"<<x<<" y:"<<y<<" num:"<<num<<" sum:"<<sum<<endl;
 52    if (num>=sum-k)
 53    {
 54	return;
 55    }
 56    for ( int i = 0 ; i < 4 ; i++)
 57    {
 58	int nx = x + dx4[i];
 59	int ny = y + dy4[i];
 60	if (num>=sum-k) break;
 61	if (ok(nx,ny))
 62	{
 63	    num++;
 64	    dfs(nx,ny);
 65	}
 66    }
 67}
 68
 69void print()
 70{
 71    for ( int i = 0 ; i < n;  i++) printf("%s\n",maze[i]);
 72}
 73int main()
 74{
 75	#ifndef  ONLINE_JUDGE
 76	freopen("code/in.txt","r",stdin);
 77  #endif
 78
 79	ms(vis,false);
 80	scanf("%d%d%d",&n,&m,&k);
 81	sum = 0 ;
 82	for ( int i = 0 ; i < n ; i++)
 83	{
 84	    scanf("%s",maze[i]);
 85	    for ( int j = 0 ; j < m; j++)
 86	    {
 87		if (maze[i][j]=='.')
 88		{
 89		    sum++;
 90		    sx = i;
 91		    sy = j;
 92		}
 93	    }
 94	}
 95//	cout<<"sum"<<sum<<endl;
 96	num = 1;
 97	dfs(sx,sy);
 98	for ( int i = 0 ; i < n ; i++)
 99	{
100	    for ( int j = 0 ; j < m;  j++)
101	    {
102		if (maze[i][j]=='.')
103		{
104		    maze[i][j]='X';
105		}
106		if (maze[i][j]=='R')
107		{
108		    maze[i][j]='.';
109		}
110	    }
111	}
112	print();
113
114  #ifndef ONLINE_JUDGE
115  fclose(stdin);
116  #endif
117    return 0;
118}

相关文章

codeforces 580 C. Kefa and Park

·394 字·1 分钟
http://codeforces.com/contest/580/problem/C 题意:给出一棵树。每个叶子节点上有一个饭店。某些节点上有cat.现在问从根节点出发可以到达多少个饭店,保证在到达饭店的路径中补连续遇到m个以上的cat.

codeforces 115A A. Party

·356 字·1 分钟
http://codeforces.com/problemset/problem/115/A 题意:给出n个人之间的上级下级关系。问如何分得最少的组,使得没一组中的人不存在上下级关系。 思路:用树的观点来考虑会很容易。可以看成给了一棵森冷。对于不同的树的相同层的点,不存在上下级关系,可以放在一个group.对于同一棵树,每一层要单独放一个group.所以答案是所有树的深度的最大值。

codeforces edu1 D. Igor In the Museum

·640 字·2 分钟
http://codeforces.com/contest/598/problem/D 题意:给第一个地图。 ‘.’是能走的,‘’是不能走的。**每个‘.’和’‘之间有一幅画,**给出k个起点,问对于每组起点,最多能观察到多少副画。

幻方....

·500 字·1 分钟
c语言上机。。。。 c写的幻方。 代码实现 1/************************************************************************* 2> File Name: code/class/7.c 3> Author: 111qqz 4> Email: rkz2013@126.com 5> Created Time: 2015年11月11日 星期三 19时31分50秒 6************************************************************************/ 7 8#include<stdio.h> 9#include <string.h> 10 11int n; 12int a[105][105]; 13 13 14 15void swap(int *a,int *b) 16{ 17int tmp; 18tmp = *a; 19*a = *b; 20*b = tmp; 21} 22int fix_x( int x,int k,int n) 23{ 24if (k%2==1) 25{ 26if (x==0) 27return n; 28else return x; 29} 30else 31{ 32if (x==n) 33return n+n; 34else return x; 35} 36} 37int fix_y ( int y,int k,int n) 38{ 39if (k<3) 40{ 41if (y==n+1) 42return 1; 43else return y; 44} 45else 46{ 47if (y==2*n+1) 48return n+1; 49else return y; 50} 51// if (y==n+1) 52// return 1; 53// else return y; 54} 55void print() 56{ 57for ( int i = 1 ; i <= n ; i++) 58{ 59for ( int j = 1 ; j <= n ; j++) 60printf("%d ",a[i][j]); 61 61 62printf("n"); 63} 64 64 65} 66 66 67void OddMagic(int n,int x,int y,int k) //k表示4中状态。。。。 68{ 69 69 70int cur ; 71if (k==1) cur = 1; 72if (k==4) cur = n*n+1; 73if (k==3) cur = n*n*2+1; 74if (k==2) cur = n*n*3+1; 75int cnt = 1; 76while (cnt<=n*n) 77{ 78a[x][y]=cur; 79int prex = x; 80int prey = y; 81cur++; 82cnt++; 83x--; 84y++; 85x = fix_x(x,k,n); 86y = fix_y(y,k,n); 87if (a[x][y]) 88{ 89x = prex+1; 90y = prey; 91} 92 92 93} 94 94 95} 96int main() 97{ 98memset(a,sizeof(a),0); 99scanf("%d",&n); 100if (n%2==1) 101{ 102int x = 1; 103int y = n/2+1; 104OddMagic(n,x,y,1); 105} 106else 107{ 108if (n%4==0) 109{ 110for ( int i = 1,num=1 ; i <= n ; i++) 111for ( int j = 1 ; j <= n ; j++,num++) 112a[i][j]=num; 113 114 115for ( int i = 1 ; i <= n ; i++) 116{ 117for ( int j = 1 ; j <= n ; j++) 118{ 119if (i==j||i+j>=n+1) continue; 120int tmp; 121tmp = a[i][j]; 122a[i][j] = a[n+1-i][n+1-j]; 123a[n+1-i][n+1-j] = tmp; 124} 125} 126} 127else 128{ 129int x = 1; 130int y = n/4+1; 131int hn = n/2; 132 133OddMagic(hn,x,y,1); 134OddMagic(hn,x+hn,y,2); 135OddMagic(hn,x,y+hn,3); 136OddMagic(hn,x+hn,y+hn,4); 137 138int m = n/4; 139for ( int i = 1 ; i <= hn ;i++) 140{ 141for ( int j = 1 ; j <= m ; j++) 142{ 143int tmp; 144if (i==m+1&&j==m) 145{ 146tmp = a[m+1][m+1]; 147a[m+1][m+1] = a[m+1+hn][m+1]; 148a[m+1+hn][m+1] = tmp; 149continue; 150 151} 152tmp = a[i][j]; 153a[i][j] = a[i+hn][j]; 154a[i+hn][j] = tmp; 155// swap(a[i][j],a[i+n][j]); 156} 157} 158 159for ( int i = 1 ; i <= hn ; i++) 160{ 161for ( int j = n ; j>=n-m+2 ; j--) 162{ 163int tmp; 164tmp = a[i][j]; 165a[i][j] = a[i+hn][j]; 166a[i+hn][j] = tmp; 167} 168} 169 170 171 172 173} 174} 175print(); 176 177}