[关闭]
@nlogn 2018-12-19T21:15:42.000000Z 字数 1734 阅读 666

Grid.cpp

  1. /* Headers */
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<iostream>
  5. #include<cctype>
  6. #include<algorithm>
  7. #include<climits>
  8. #include<vector>
  9. /* define */
  10. typedef std::pair<int,int> point;
  11. /* consts */
  12. const int MAXN = 2010;
  13. using namespace std;
  14. namespace FastIO{
  15. const int BUFSIZE=1<<20;
  16. char ibuf[BUFSIZE],*is=ibuf,*it=ibuf;
  17. char obuf[BUFSIZE],*os=obuf,*ot=obuf+BUFSIZE;
  18. inline char getch(){
  19. if(is==it)
  20. it=(is=ibuf)+fread(ibuf,1,BUFSIZE,stdin);
  21. return (is==it)?EOF:*is++;
  22. }
  23. inline int getint(){
  24. int res=0,neg=0,ch=getch();
  25. while(!(isdigit(ch)||ch=='-')&&ch!=EOF)
  26. ch=getch();
  27. if(ch=='-'){
  28. neg=1;ch=getch();
  29. }
  30. while(isdigit(ch)){
  31. res=(res<<3)+(res<<1)+(ch-'0');
  32. ch=getch();
  33. }
  34. return neg?-res:res;
  35. }
  36. inline void flush(){
  37. fwrite(obuf,1,os-obuf,stdout);
  38. os=obuf;
  39. }
  40. inline void putch(char ch){
  41. *os++=ch;
  42. if(os==ot) flush();
  43. }
  44. inline void putint(int res){
  45. static char q[10];
  46. if(res==0) putch('0');
  47. else if(res<0){
  48. putch('-');
  49. res=-res;
  50. }
  51. int top=0;
  52. while(res){
  53. q[top++]=res%10+'0';
  54. res/=10;
  55. }
  56. while(top--) putch(q[top]);
  57. }
  58. inline void space(int x){
  59. if(!x)puts("");
  60. else putchar(' ');
  61. }
  62. }
  63. using namespace FastIO;
  64. /* defintions */
  65. char word[MAXN][MAXN];
  66. int n,m;
  67. bool visited[MAXN][MAXN];
  68. /* functions */
  69. inline void init(){
  70. scanf("%d%d",&n,&m);
  71. for(int i=0;i<n;i++){
  72. cin>>word[i];
  73. }
  74. }
  75. inline void find_answer(){
  76. init();
  77. vector<point>v,nexts;
  78. for(v.push_back({0, 0}); !v.empty();v = nexts){
  79. point p=v.back();
  80. cout<<word[p.first][p.second];
  81. char another='z';
  82. for(point k : v){
  83. int x=1,y=0;
  84. for(int i=0;i<2;++i){
  85. swap(x,y);
  86. int dx=k.first+x;
  87. int dy=k.second+y;
  88. if(dx>=n||dy>=m)continue;
  89. another=min(another,word[dx][dy]);
  90. }
  91. }
  92. nexts.clear();
  93. for(point k : v){
  94. int x=1,y=0;
  95. for(int i=0;i<2;++i){
  96. swap(x,y);
  97. int dx=k.first+x;
  98. int dy=k.second+y;
  99. if(dx>=n||dy>=m)continue;
  100. if(visited[dx][dy])continue;
  101. if(word[dx][dy]==another){
  102. nexts.push_back({dx,dy});
  103. visited[dx][dy]=1;
  104. }
  105. }
  106. }
  107. }
  108. }
  109. int main(void){
  110. find_answer();
  111. space(0);
  112. return 0;
  113. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注