【A】-_-///
【B】线段树+位运算(感觉可出)
【E】-_-///
【F】三分+圆与线段的交点,计算几何
【G】-_-///
【H】线段树+树链剖分
【I】后缀数组+二分
【J】DFS搜索
这场网赛当时自己完成了的也就是两道地图题,过去好久了才想到还是该记录下来...
【C】
Wang Xifeng's Little Plot
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
In the novel, Wang Xifeng was in charge of Da Guan Yuan, where people of Jia family lived. It was mentioned in the newly recovered pages that Wang Xifeng used to arrange rooms for Jia Baoyu, Lin Daiyu, Xue Baochai and other teenagers. Because Jia Baoyu was the most important inheritor of Jia family, and Xue Baochai was beautiful and very capable , Wang Xifeng didn't want Jia Baoyu to marry Xue Baochai, in case that Xue Baochai might take her place. So, Wang Xifeng wanted Baoyu's room and Baochai's room to be located at two ends of a road, and this road should be as long as possible. But Baoyu was very bad at directions, and he demanded that there could be at most one turn along the road from his room to Baochai's room, and if there was a turn, that turn must be ninety degree. There is a map of Da Guan Yuan in the novel, and redists (In China English, one whose job is studying 《Dream of the Red Chamber》is call a "redist") are always arguing about the location of Baoyu's room and Baochai's room. Now you can solve this big problem and then become a great redist.
There are several test cases.
For each case, the first line is an integer N(0<N<=100) ,meaning the map is a N × N matrix.
Then the N × N matrix follows.
The input ends with N = 0.
#.#
##.
..# ...
##.
..# ...
###
..# ...
##.
...
【Sample Output】
【题意】
给出一张图,'.'表示路,'#'表示墙,要求找出一条最长的路,路径上只允许转弯一次。
【分析】
本题首先应该看到虽然要求是可以向八个方向走,但是由于转弯只能是90°的直角弯,所以上下左右四个方向与另外四个方向是完全独立的。
所以直观的想法是分成两组进行BFS,从起点开始(起点应该为某一条路的尽头),记录下上一个点到达当前点的方向、是否转过弯,每一次向下一个点搜索的方向只有三个,记录沿途的最长距离即可。
这道题的代码写得太朴素了,方向这里写了一次复制了7遍,,,都不好意思贴出来了,,,-_-///
/* ***********************************************
MYID : Chen Fan
LANG : G++
PROG : C1003
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; int dx1[]={-,,,-};
int dy1[]={-,-,,};
int dx2[]={-,,,};
int dy2[]={,-,,};
int ans;
bool ma[][];
int num[][]; bool check(int xx,int yy)
{
int temp1=,temp2=;
for (int i=;i<;i++)
{
temp1+=ma[xx+dx1[i]][yy+dy1[i]];
temp2+=ma[xx+dx2[i]][yy+dy2[i]];
}
if (temp1>||temp2>) return true;
else return false;
} typedef struct nod{
int x,y,fx;
bool zw;
} node;
node q[]; void doit(int xx,int yy)
{
int head=,tail=;
memset(num,,sizeof(num));
q[head].x=xx;
q[head].y=yy;
q[head].fx=-;
q[head].zw=false;
num[xx][yy]=;
while (head<=tail)
{
if (ma[q[head].x-][q[head].y-]&&num[q[head].x-][q[head].y-]==&&(q[head].fx==-||q[head].fx==||q[head].fx==||q[head].fx==))
{
tail++;
q[tail].x=q[head].x-;
q[tail].y=q[head].y-;
if (q[head].fx==||q[head].fx==-)
{
q[tail].fx=;
q[tail].zw=q[head].zw;
num[q[tail].x][q[tail].y]=num[q[head].x][q[head].y]+;
if (ans<num[q[tail].x][q[tail].y]) ans=num[q[tail].x][q[tail].y];
} else
if (q[head].zw==false)
{
q[tail].fx=;
q[tail].zw=true;
num[q[tail].x][q[tail].y]=num[q[head].x][q[head].y]+;
if (ans<num[q[tail].x][q[tail].y]) ans=num[q[tail].x][q[tail].y];
} else tail--;
}
if (ma[q[head].x-][q[head].y+]&&num[q[head].x-][q[head].y+]==&&(q[head].fx==-||q[head].fx==||q[head].fx==||q[head].fx==))
{
tail++;
q[tail].x=q[head].x-;
q[tail].y=q[head].y+;
if (q[head].fx==||q[head].fx==-)
{
q[tail].fx=;
q[tail].zw=q[head].zw;
num[q[tail].x][q[tail].y]=num[q[head].x][q[head].y]+;
if (ans<num[q[tail].x][q[tail].y]) ans=num[q[tail].x][q[tail].y];
} else
if (q[head].zw==false)
{
q[tail].fx=;
q[tail].zw=true;
num[q[tail].x][q[tail].y]=num[q[head].x][q[head].y]+;
if (ans<num[q[tail].x][q[tail].y]) ans=num[q[tail].x][q[tail].y];
} else tail--;
}
if (ma[q[head].x+][q[head].y-]&&num[q[head].x+][q[head].y-]==&&(q[head].fx==-||q[head].fx==||q[head].fx==||q[head].fx==))
{
tail++;
q[tail].x=q[head].x+;
q[tail].y=q[head].y-;
if (q[head].fx==||q[head].fx==-)
{
q[tail].fx=;
q[tail].zw=q[head].zw;
num[q[tail].x][q[tail].y]=num[q[head].x][q[head].y]+;
if (ans<num[q[tail].x][q[tail].y]) ans=num[q[tail].x][q[tail].y];
} else
if (q[head].zw==false)
{
q[tail].fx=;
q[tail].zw=true;
num[q[tail].x][q[tail].y]=num[q[head].x][q[head].y]+;
if (ans<num[q[tail].x][q[tail].y]) ans=num[q[tail].x][q[tail].y];
} else tail--; }
if (ma[q[head].x+][q[head].y+]&&num[q[head].x+][q[head].y+]==&&(q[head].fx==-||q[head].fx==||q[head].fx==||q[head].fx==))
{
tail++;
q[tail].x=q[head].x+;
q[tail].y=q[head].y+;
if (q[head].fx==||q[head].fx==-)
{
q[tail].fx=;
q[tail].zw=q[head].zw;
num[q[tail].x][q[tail].y]=num[q[head].x][q[head].y]+;
if (ans<num[q[tail].x][q[tail].y]) ans=num[q[tail].x][q[tail].y];
} else
if (q[head].zw==false)
{
q[tail].fx=;
q[tail].zw=true;
num[q[tail].x][q[tail].y]=num[q[head].x][q[head].y]+;
if (ans<num[q[tail].x][q[tail].y]) ans=num[q[tail].x][q[tail].y];
} else tail--;
}
head++;
} head=;tail=;
memset(num,,sizeof(num));
num[xx][yy]=;
while (head<=tail)
{
if (ma[q[head].x-][q[head].y]&&num[q[head].x-][q[head].y]==&&(q[head].fx==-||q[head].fx==||q[head].fx==||q[head].fx==))
{
tail++;
q[tail].x=q[head].x-;
q[tail].y=q[head].y;
if (q[head].fx==||q[head].fx==-)
{
q[tail].fx=;
q[tail].zw=q[head].zw;
num[q[tail].x][q[tail].y]=num[q[head].x][q[head].y]+;
if (ans<num[q[tail].x][q[tail].y]) ans=num[q[tail].x][q[tail].y];
} else
if (q[head].zw==false)
{
q[tail].fx=;
q[tail].zw=true;
num[q[tail].x][q[tail].y]=num[q[head].x][q[head].y]+;
if (ans<num[q[tail].x][q[tail].y]) ans=num[q[tail].x][q[tail].y];
} else tail--;
}
if (ma[q[head].x][q[head].y-]&&num[q[head].x][q[head].y-]==&&(q[head].fx==-||q[head].fx==||q[head].fx==||q[head].fx==))
{
tail++;
q[tail].x=q[head].x;
q[tail].y=q[head].y-;
if (q[head].fx==||q[head].fx==-)
{
q[tail].fx=;
q[tail].zw=q[head].zw;
num[q[tail].x][q[tail].y]=num[q[head].x][q[head].y]+;
if (ans<num[q[tail].x][q[tail].y]) ans=num[q[tail].x][q[tail].y];
} else
if (q[head].zw==false)
{
q[tail].fx=;
q[tail].zw=true;
num[q[tail].x][q[tail].y]=num[q[head].x][q[head].y]+;
if (ans<num[q[tail].x][q[tail].y]) ans=num[q[tail].x][q[tail].y];
} else tail--;
}
if (ma[q[head].x][q[head].y+]&&num[q[head].x][q[head].y+]==&&(q[head].fx==-||q[head].fx==||q[head].fx==||q[head].fx==))
{
tail++;
q[tail].x=q[head].x;
q[tail].y=q[head].y+;
if (q[head].fx==||q[head].fx==-)
{
q[tail].fx=;
q[tail].zw=q[head].zw;
num[q[tail].x][q[tail].y]=num[q[head].x][q[head].y]+;
if (ans<num[q[tail].x][q[tail].y]) ans=num[q[tail].x][q[tail].y];
} else
if (q[head].zw==false)
{
q[tail].fx=;
q[tail].zw=true;
num[q[tail].x][q[tail].y]=num[q[head].x][q[head].y]+;
if (ans<num[q[tail].x][q[tail].y]) ans=num[q[tail].x][q[tail].y];
} else tail--; }
if (ma[q[head].x+][q[head].y]&&num[q[head].x+][q[head].y]==&&(q[head].fx==-||q[head].fx==||q[head].fx==||q[head].fx==))
{
tail++;
q[tail].x=q[head].x+;
q[tail].y=q[head].y;
if (q[head].fx==||q[head].fx==-)
{
q[tail].fx=;
q[tail].zw=q[head].zw;
num[q[tail].x][q[tail].y]=num[q[head].x][q[head].y]+;
if (ans<num[q[tail].x][q[tail].y]) ans=num[q[tail].x][q[tail].y];
} else
if (q[head].zw==false)
{
q[tail].fx=;
q[tail].zw=true;
num[q[tail].x][q[tail].y]=num[q[head].x][q[head].y]+;
if (ans<num[q[tail].x][q[tail].y]) ans=num[q[tail].x][q[tail].y];
} else tail--;
}
head++;
}
} int main()
{
//freopen("C1003.txt","r",stdin); int n;
scanf("%d",&n);
while (n)
{
getchar();
memset(ma,,sizeof(ma));
for (int i=;i<=n;i++)
{
for (int j=;j<=n;j++)
{
char c;
scanf("%c",&c);
if (c=='.') ma[i][j]=true;
}
getchar();
} ans=;
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
if (ma[i][j]&&check(i,j)) doit(i,j); printf("%d\n",ans);
scanf("%d",&n);
} return ;
}
【启发】
对于这种初看8个方向,然后相互没有关系的一定注意分开考虑。
【D】
Saving Tang Monk
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
During the journey, Tang Monk was often captured by demons. Most of demons wanted to eat Tang Monk to achieve immortality, but some female demons just wanted to marry him because he was handsome. So, fighting demons and saving Monk Tang is the major job for Sun Wukong to do.
Once, Tang Monk was captured by the demon White Bones. White Bones lived in a palace and she cuffed Tang Monk in a room. Sun Wukong managed to get into the palace. But to rescue Tang Monk, Sun Wukong might need to get some keys and kill some snakes in his way.
The palace can be described as a matrix of characters. Each character stands for a room. In the matrix, 'K' represents the original position of Sun Wukong, 'T' represents the location of Tang Monk and 'S' stands for a room with a snake in it. Please note that there are only one 'K' and one 'T', and at most five snakes in the palace. And, '.' means a clear room as well '#' means a deadly room which Sun Wukong couldn't get in.
There may be some keys of different kinds scattered in the rooms, but there is at most one key in one room. There are at most 9 kinds of keys. A room with a key in it is represented by a digit(from '1' to '9'). For example, '1' means a room with a first kind key, '2' means a room with a second kind key, '3' means a room with a third kind key... etc. To save Tang Monk, Sun Wukong must get ALL kinds of keys(in other words, at least one key for each kind).
For each step, Sun Wukong could move to the adjacent rooms(except deadly rooms) in 4 directions(north, west, south and east), and each step took him one minute. If he entered a room in which a living snake stayed, he must kill the snake. Killing a snake also took one minute. If Sun Wukong entered a room where there is a key of kind N, Sun would get that key if and only if he had already got keys of kind 1,kind 2 ... and kind N-1. In other words, Sun Wukong must get a key of kind N before he could get a key of kind N+1 (N>=1). If Sun Wukong got all keys he needed and entered the room in which Tang Monk was cuffed, the rescue mission is completed. If Sun Wukong didn't get enough keys, he still could pass through Tang Monk's room. Since Sun Wukong was a impatient monkey, he wanted to save Tang Monk as quickly as possible. Please figure out the minimum time Sun Wukong needed to rescue Tang Monk.
For each case, the first line includes two integers N and M(0 < N <= 100, 0<=M<=9), meaning that the palace is a N×N matrix and Sun Wukong needed M kinds of keys(kind 1, kind 2, ... kind M).
Then the N × N matrix follows.
The input ends with N = 0 and M = 0.
K.S
##
#T K#T
.S#
#. K#T
.S.
.
【Sample Output】
impossible
【题意】
又是一道地图题,题目要求为从起点(悟空的位置)出发,按顺序通过m个点,最后到达终点(唐僧的位置),最后输出impossible或者全程的最短距离。
【分析】
当时看到题目就傻了好久,需要处理的问题太多:
1.钥匙可能有多把,即地图上好多个1,好多个2......
2.蛇的问题,刚开始看的时候还没觉得什么,以为标记一下经过的时候时间+1即可,后来发现有点不对,看了比赛时其他人的提问才知道原来我觉得不对的地方是真不对,有蛇的点经过一次之后蛇就会被杀死!!下次再经过就成了普通点!!
首先解决第一个问题,BFS搜距离+DFS选择路径:
这张图的大小最大只有100*100,总点数最大是10000,考虑最坏情况,即图中的所有点中都有钥匙,9种钥匙均匀分布大约是(9998/9=1110),然后1000^9是......///-_-///......妈蛋,当时没考虑这么多,现在想想如果真的碰到这么大的数据量,我这么做真是早就爆了......
我把所有钥匙相同的点都编为1组,然后用BFS搜出相邻两组所有点对之间的距离,最后从起点(第0组)开始,DFS找出到达终点(第m+1组)的路径。
接下来是第二个更加棘手的情况:
分析下面这张图:
K....
.....
..s..
.....
....T
从左上角到达右下角每一点的距离:
发现一个重要规律!即如果能够不走有蛇的点,那么一定能找出一条同样短的路不经过有蛇点。也就是说,若是最短路中经过了有蛇点,那么这个点是必定要走的,否则另外找一条路花费的代价会比消灭这条蛇的1点时间更大,即例如出现下面这种情况。
K....
.....
#S##.
.....
T....
穿越蛇点的代价肯定比绕开走要小。
然后看到题目特别指出最多只会出现5条蛇,于是就有了我下面的尝试算法:
1.将所有的蛇拿掉,做一次BFS+DFS操作,得到一个完成全程的最短距离ans;
2.放上一条蛇(把那个点标记为耗时2),再做一次BFS+DFS操作,得到一个完成全程的最短距离ans0。若ans0比ans更大,说明这个有蛇点是必须要经过的,记录一下,若ans0与ans相等则说明该点可以不经过,不影响结果;
3.拿掉之前的那条蛇,放上下一条蛇,并依此完成以上操作,判断每一个有蛇点是否都是必须经过的,不断记录即可。
最终答案为ans加上必须要经过的蛇点的个数
这里最为担心的是完成一次BFS+DFS操作需要的耗时问题,就像上面提到的,不用说极限情况,可能稍微接近一些极限的数据出来就会TLE,更不用说下面还要把这种操作再重复5次了。
幸运的是最后木有超...只能说出题的大大给的数据还没有到灭绝弱队的地步(哭)
hdu这道题有耗时更少的,一会再去找找其他神牛的题解好了。
/* ***********************************************
MYID : Chen Fan
LANG : G++
PROG : D1004
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; typedef struct nod{
int x,y;
} node;
node lis[][];
node q[];
int an[][][];
int ma[][],num[][];
bool flag[][];
bool state[][][];
bool done;
int ns;
node ss[]; int n,m,ans; int dx[]={-,,,};
int dy[]={,-,,}; int doit(int sx,int sy,int ex,int ey)
{
int head=,tail=,ret=;
q[head].x=sx;
q[head].y=sy;
memset(num,,sizeof(num));
memset(flag,,sizeof(flag));
flag[q[head].x][q[head].y]=true;
bool done=false;
while (head<=tail)
{
if (q[head].x==ex&&q[head].y==ey)
{
done=true;
ret=num[q[head].x][q[head].y];
break;
}
for (int i=;i<;i++)
if (ma[q[head].x+dx[i]][q[head].y+dy[i]]&&(num[q[head].x+dx[i]][q[head].y+dy[i]]==||num[q[head].x+dx[i]][q[head].y+dy[i]]>num[q[head].x][q[head].y]+ma[q[head].x+dx[i]][q[head].y+dy[i]]))
{
num[q[head].x+dx[i]][q[head].y+dy[i]]=num[q[head].x][q[head].y]+ma[q[head].x+dx[i]][q[head].y+dy[i]];
if (!flag[q[head].x+dx[i]][q[head].y+dy[i]])
{
tail++;
q[tail].x=q[head].x+dx[i];
q[tail].y=q[head].y+dy[i];
flag[q[head].x+dx[i]][q[head].y+dy[i]]=true;
}
}
flag[q[head].x][q[head].y]=false;
head++;
}
if (done) return ret;
else return -;
} void dfs(int s,int t,int tot)
{
if (s==m+)
{
if (ans>tot)
{
ans=tot;
done=true;
}
return;
}
for (int i=;i<=lis[s+][].x;i++)
if (an[s][t][i]!=-)
{
dfs(s+,i,tot+an[s][t][i]);
}
} int main()
{
//freopen("D1004.txt","r",stdin); scanf("%d%d",&n,&m);
while (!(n==&&m==))
{
getchar();
memset(ma,,sizeof(ma));
memset(lis,,sizeof(lis));
ns=;
for (int i=;i<=n;i++)
{
for (int j=;j<=n;j++)
{
char c;
scanf("%c",&c);
switch(c)
{
case 'K':
lis[][].x=;
lis[][].x=i;lis[][].y=j;
ma[i][j]=;
break;
case 'T':
lis[m+][].x=;
lis[m+][].x=i;lis[m+][].y=j;
ma[i][j]=;
break;
case 'S':
ns++;
ss[ns].x=i;ss[ns].y=j;
ma[i][j]=;
break;
case '.':
ma[i][j]=;
break;
case '#':
break;
default:
ma[i][j]=;
lis[c-''][].x++;
lis[c-''][lis[c-''][].x].x=i;
lis[c-''][lis[c-''][].x].y=j;
}
}
getchar();
} ans=;
memset(an,,sizeof(an));
for (int i=;i<=m;i++)
{
for (int j=;j<=lis[i][].x;j++)
for (int k=;k<=lis[i+][].x;k++)
an[i][j][k]=doit(lis[i][j].x,lis[i][j].y,lis[i+][k].x,lis[i+][k].y);
}
done=false;
ans=;
dfs(,,);
if (done)
{
int ans0=ans;
int temp=;
for (int p=;p<=ns;p++)
{
if (p>) ma[ss[p-].x][ss[p-].y]=;
ans=;
ma[ss[p].x][ss[p].y]=;
memset(an,,sizeof(an));
for (int i=;i<=m;i++)
{
for (int j=;j<=lis[i][].x;j++)
for (int k=;k<=lis[i+][].x;k++)
an[i][j][k]=doit(lis[i][j].x,lis[i][j].y,lis[i+][k].x,lis[i+][k].y);
}
done=false;
ans=;
dfs(,,);
if (ans>ans0) temp++;
}
printf("%d\n",ans0+temp);
}
else printf("impossible\n"); scanf("%d%d",&n,&m);
} return ;
}