fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int t;
  7. cin>>t;
  8. while(t--)
  9. {
  10. int n,m;
  11. cin>>n>>m;
  12. vector<string> v(n);
  13. for(int i=0;i<n;i++)
  14. cin>>v[i];
  15.  
  16. if(m%2==1)
  17. {
  18. cout<<"YES"<<endl;
  19. for(int i=0;i<=m;i++)
  20. {
  21. if(i%2==0)
  22. cout<<1<<" ";
  23. else cout<<2<<" ";
  24. }
  25. cout<<endl;
  26. continue;
  27. }
  28.  
  29. int a=-1, b=-1;
  30. bool x=0;
  31. for(int i=0;i<n && x==0;i++)
  32. {
  33. for(int j=i+1;j<n;j++)
  34. {
  35. if(v[j][i]==v[i][j])
  36. {
  37. a=i;b=j;x=1;
  38. break;
  39. }
  40. }
  41. }
  42. if(x==1)
  43. {
  44. cout<<"YES"<<endl;
  45. for(int i=0;i<=m;i++)
  46. {
  47. if(i%2==0)
  48. cout<<a+1<<" ";
  49. else cout<<b+1<<" ";
  50. }
  51. cout<<endl;
  52. continue;
  53. }
  54. if(n==2)
  55. {
  56. cout<<"NO"<<endl;
  57. continue;
  58. }
  59. int c;
  60. if(v[0][1] != v[0][2])
  61. {
  62. c=1;
  63. a=2;
  64. b=3;
  65. }
  66. else if(v[1][0]!=v[1][2])
  67. {
  68. c=2;
  69. a=1;
  70. b=3;
  71. }
  72. else{
  73. c=3;
  74. a=1;
  75. b=2;
  76. }
  77. cout<<"YES"<<endl;
  78. if(m%4==2)
  79. cout<<a<<" ";
  80. for(int i=0;i<(m-2)/4;i++)
  81. {
  82. cout<<c<<" "<<a<<" "<<c<<" "<<b<<" ";
  83. }
  84. cout<<c<<" ";
  85. if(m%4==2)cout<<b;
  86. cout<<endl;
  87. }
  88. return 0;
  89. }
Success #stdin #stdout 0s 4900KB
stdin
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
stdout
YES
1 2 
YES
1 2 1 2 
YES
1 2 1 2 1 
YES
1 3 1 3 1 3 1 
NO