hust 1625 Chessboard

题目描述

Given an N*N(N<=1000)chessboard where you want to place chess
knights.

On this chessboard you have to apply M(M<=100000) operations:

输入

The first line contains a single integer T, the number of test cases.

For each case,

The first line contains integer N, M.

The next M lines contain the operation in following form.

C a b x: place chess knight on cell(a,b), the value of the knight is x.
(1<=a,b<=n, 1<=x<=100000)

It grants that cell(a,b) has no knight before the operation.

Q a b: answer the maximum value of knight which is connected with
knight(a,b), include itself.

If cell (a,b)has no knight, just answer -1. A knight is directly connected
with the knight on the left,

right,  up  and  down.  Two  knights  are
 connected  if  they  have  been  directly
 connected  or

interconnected through some other connected knights.

The initial chessboard is empty.

输出

For each question, output the answer in one line.

样例输入

1
3 7
C 2 2 1
Q 2 2
C 1 2 2
Q 2 2
C 3 3 3
Q 3 3
Q 1 1

样例输出

1
2
3
-1

一看题目,想到的方法就是BFS,然后就写了,


#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=1001;
int group[maxn][maxn];
bool vis[maxn][maxn];
int dx[4]={0,0,1,-1};
int dy[4]={-1,1,0,0};

struct node
{
int x,y;
};

queue<node>q;

int main()
{
int t,MAX,a,b,v,n,m,mm;
char str[2];
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
memset(group,0,sizeof(group));
mm=0;
while(m--)
{
scanf("%s",str);
if (str[0]==‘C‘)
{
scanf("%d%d%d",&a,&b,&v);
group[a][b]=v;
mm=max(mm,v);
}
else
{
scanf("%d%d",&a,&b);
if (group[a][b]==0) printf("-1\n");
else
{
MAX=group[a][b];
if (mm==MAX)
{
printf("%d\n",mm); continue;
}
memset(vis,0,sizeof(vis));
node temp;
temp.x=a; temp.y=b;
vis[a][b]=true;
q.push(temp);
while(!q.empty())
{
if (mm==MAX)
{
q.pop();
continue;
}
temp=q.front(); q.pop();
for (int i=0;i<4;i++)
{
int x=temp.x+dx[i];
int y=temp.y+dy[i];
if(!vis[x][y] && group[x][y]>0 && x>=1 && x<=n && y>=1 && y<=n)
{
vis[x][y]=true;
MAX=max(MAX,group[x][y]);
node temp1;
temp1.x=x; temp1.y=y;
q.push(temp1);
}
}
}
printf("%d\n",MAX);
}
}
}
}
return 0;
}

但是大家都知道,超时了,后来想了想,完全符合并查集的知识啊


#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=2001;

int n,m;
struct node
{
int root,MAX;
}p[maxn*maxn];

int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};

int find(int x)
{
return p[x].root==x?x:p[x].root=find(p[x].root);
}

void init()
{
int nn=n*n;
for (int i=0;i<=nn;i++)
{
p[i].root=i;
p[i].MAX=0;
}
}

int main()
{
int t,a,b,v;
char str[2];
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init();
while (m--)
{
scanf("%s",str);
if (str[0]==‘C‘)
{
scanf("%d%d%d",&a,&b,&v);
int temp2=find((a-1)*n+b);
p[temp2].MAX=max(v,p[temp2].MAX);
for (int i=0;i<4;i++)
{
int xx=a+dx[i];
int yy=b+dy[i];
if (xx>=1 && xx<=n && yy>=1 && yy<=n && p[find((xx-1)*n+yy)].MAX>0)
{
int temp1=find((xx-1)*n+yy);
if (temp1!=temp2) p[temp1].root=p[temp2].root;
p[temp2].MAX=max(p[temp1].MAX,p[temp2].MAX);
}
}
}
else
{
scanf("%d%d",&a,&b);
int temp=find((a-1)*n+b);
if (p[temp].MAX>0)printf("%d\n",p[temp].MAX);
else printf("-1\n");
}
}
}
return 0;
}

hust 1625 Chessboard

时间: 2024-10-06 09:51:44

hust 1625 Chessboard的相关文章

POJ——T2446 Chessboard

http://poj.org/problem?id=2446 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18560   Accepted: 5857 Description Alice and Bob often play games on chessboard. One day, Alice draws a board with size M * N. She wants Bob to use a lot of c

HUST 1588 辗转数对

1588 - 辗转数对 时间限制:1秒 内存限制:128兆 155 次提交 27 次通过 题目描述 假设当前有一个数对(a, b),我们可以通过一步将这个数对变为一个新数对(a + b, b)或者是(a, a + b).初始的数对为(1, 1),你的任务是找到一个数字k,即通过最少的步数使得这个数对中至少一个数字等于n. 输入 输入包括多组数据,每组数据包括一行,每行有一个整数n. 输出 每组数据输出一行,每行一个整数n. 样例输入 5 3 样例输出 3 2 提示 第一个样例的方法是 (1,1)

HUST 1698 - 电影院 组合数学 + 分类思想

http://acm.hust.edu.cn/problem/show/1698 题目就是要把一个数n分成4段,其中中间两段一定要是奇数. 问有多少种情况. 分类, 奇数 + 奇数 + 奇数 + 奇数 奇数 + 奇数 + 奇数 + 偶数 偶数 + 奇数 + 奇数 + 奇数 偶数 + 奇数 + 奇数 + 偶数 然后奇数表达成 2 * a - 1这个样子,就能列出方程. 然后就是类似于解a1 + a2 + a3 + a4 = x的问题了. #include <cstdio> #include &l

HUST 1017 - Exact cover (Dancing Links 模板题)

1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 5584 次提交 2975 次通过 题目描述 There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is a selection of rows such that every column has a 1 in exactly one of the selected rows. Try to find o

勇士出征[HUST 1439]

勇士出征[HUST 1439] 时间1000ms,内存64MB 第十届"北大青鸟"杯浙江师范大学程序设计竞赛 这道题跟UVA-12100是一样的题目.我这里用的是STL的双端队列deque容器配合优先队列priority_queue,写起来会比较轻松:依次将输入压入队列,然后不断扫描队列,符合最大优先级的(优先队列的顶部元素)将其送出,而不再压入队尾.直到找到符合自己的标记的为止. 当然这道题也有用数组使用滚雪球的方式实现的,也就是开一个大的数组,每次将元素后挪时,直接将其放在数组末尾

UVALive 2659+HUST 1017+ZOJ 3209 (DLX

UVALive 2659 题目:16*16的数独.试了一发大白模板. /* * @author: Cwind */ //#pragma comment(linker, "/STACK:102400000,102400000") #include <iostream> #include <map> #include <algorithm> #include <cstdio> #include <cstring> #include

HUST 1341 A - A Simple Task(哈理工 亚洲区选拔赛练习赛)

A - A Simple Task Time Limit:1000MS    Memory Limit:131072KB    64bit IO Format:%lld & %llu SubmitStatusPracticeHUST 1341 Description As is known to all, lots of birds are living in HUST. A bird has s units of food on the first day, and eats k units

poj 1625 Censored!(AC自动机+DP+高精度)

题目链接:poj 1625 Censored! 题目大意:给定N,M,K,然后给定一个N字符的字符集和,现在要用这些字符组成一个长度为M的字符串,要求不包 括K个子字符串. 解题思路:AC自动机+DP+高精度.这题恶心的要死,给定的不能匹配字符串里面有负数的字符情况,也算是涨姿势 了,对应每个字符固定偏移128单位. #include <cstdio> #include <cstring> #include <queue> #include <vector>

POJ 3344 &amp; HDU 2414 Chessboard Dance(模拟)

题目链接: PKU:http://poj.org/problem?id=3344 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2414 Description Another boring Friday afternoon, Betty the Beetle thinks how to amuse herself. She goes out of her hiding place to take a walk around the living r